Codeforces Round #363 D Fix a Tree(并查集)
2016-07-20 19:46
489 查看
D. Fix a Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A tree is an undirected connected graph without cycles.
Let's consider a rooted undirected tree with n vertices, numbered 1 through n.
There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn,
where pi denotes
a parent of vertex i (here, for convenience a root is considered its own parent).
For
this rooted tree the array p is [2, 3, 3, 2].
Given a sequence p1, p2, ..., pn,
one is able to restore a tree:
There must be exactly one index r that pr = r.
A vertex r is a root of the tree.
For all other n - 1 vertices i,
there is an edge between vertex i and vertex pi.
A sequence p1, p2, ..., pn is
called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences(1,2,2), (2,3,1) and (2,1,3) are
not valid.
You are given a sequence a1, a2, ..., an,
not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable
in the minimum number of changes, print any of them.
Input
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) —
the number of vertices in the tree.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
Output
In the first line print the minimum number of elements to change, in order to get a valid sequence.
In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in
the minimum number of changes. If there are many such sequences, any of them will be accepted.
Examples
input
output
input
output
input
output
Note
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4(because p4 = 4),
which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right
drawing below). On both drawings, roots are painted red.
In the second sample, the given sequence is already valid.
题意:给你一个不完全的图,问你最少移动多少节点可以使图变成树。
思路:先用并查集判断该图是否有环,如果没环,那就是树。有环的话,先判断这些环中是否只有一个根节点的环,然后将其他环全部加到这个环上。如果没有只有一个根节点的环,那么就任意将某一条环拆成一个树,再将其他环都拆掉加到这棵树中。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef __int64 ll;
int fa[200005],a[200005],ans[200005];
int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void Union(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
fa[fx]=fy;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
fa[i]=i;
int cnt=0,t=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
int fx=find(i),fy=find(a[i]);
if(fx==fy)//判断是否有环
{
ans[cnt++]=i;
if(a[i]==i) t=fx;//判断该环是否只有一个根节点
}
Union(fx,fy);
}
if(t==0)
{
for(int i=1;i<=n;i++)
{
int fx=find(i),fy=find(a[i]);
if(fx==fy) t=fx;
}
}
int cnt1=0;
for(int i=0;i<cnt;i++)
{
if(a[ans[i]]!=t)
{
cnt1++;
a[ans[i]]=t;
}
}
cout<<cnt1<<endl;
for(int i=1;i<=n;i++)
printf("%d ",a[i]);
printf("\n");
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A tree is an undirected connected graph without cycles.
Let's consider a rooted undirected tree with n vertices, numbered 1 through n.
There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn,
where pi denotes
a parent of vertex i (here, for convenience a root is considered its own parent).
For
this rooted tree the array p is [2, 3, 3, 2].
Given a sequence p1, p2, ..., pn,
one is able to restore a tree:
There must be exactly one index r that pr = r.
A vertex r is a root of the tree.
For all other n - 1 vertices i,
there is an edge between vertex i and vertex pi.
A sequence p1, p2, ..., pn is
called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences(1,2,2), (2,3,1) and (2,1,3) are
not valid.
You are given a sequence a1, a2, ..., an,
not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable
in the minimum number of changes, print any of them.
Input
The first line of the input contains an integer n (2 ≤ n ≤ 200 000) —
the number of vertices in the tree.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).
Output
In the first line print the minimum number of elements to change, in order to get a valid sequence.
In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in
the minimum number of changes. If there are many such sequences, any of them will be accepted.
Examples
input
4 2 3 3 4
output
1 2 3 4 4
input
5 3 2 2 5 3
output
0 3 2 2 5 3
input
8 2 3 5 4 1 6 6 7
output
2 2 3 7 8 1 6 6 7
Note
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4(because p4 = 4),
which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right
drawing below). On both drawings, roots are painted red.
In the second sample, the given sequence is already valid.
题意:给你一个不完全的图,问你最少移动多少节点可以使图变成树。
思路:先用并查集判断该图是否有环,如果没环,那就是树。有环的话,先判断这些环中是否只有一个根节点的环,然后将其他环全部加到这个环上。如果没有只有一个根节点的环,那么就任意将某一条环拆成一个树,再将其他环都拆掉加到这棵树中。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef __int64 ll;
int fa[200005],a[200005],ans[200005];
int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void Union(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
fa[fx]=fy;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
fa[i]=i;
int cnt=0,t=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
int fx=find(i),fy=find(a[i]);
if(fx==fy)//判断是否有环
{
ans[cnt++]=i;
if(a[i]==i) t=fx;//判断该环是否只有一个根节点
}
Union(fx,fy);
}
if(t==0)
{
for(int i=1;i<=n;i++)
{
int fx=find(i),fy=find(a[i]);
if(fx==fy) t=fx;
}
}
int cnt1=0;
for(int i=0;i<cnt;i++)
{
if(a[ans[i]]!=t)
{
cnt1++;
a[ans[i]]=t;
}
}
cout<<cnt1<<endl;
for(int i=1;i<=n;i++)
printf("%d ",a[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- UVA 657 The die is cast
- (面试)指针与数组的区别
- Simplify Path
- codeforces C. Hometask(贪心)
- [置顶] Android6.0权限适配的那些坑
- hdu 5726 GCD 倍增+ 二分
- iOS-RAC学习笔记(一)——RACStream
- TotoiseSVN-小乌龟的使用方法
- 交叉编译Qt5.5.1 with egl (2)
- HDOJ 1002 A + B Problem II
- 用IntelliJ IDEA创建Gradle项目简单入门
- 指针和数组的区别
- 【杭电-oj】-4548-美素数(打表)
- poj 1062 昂贵的聘礼(DFS)
- Mac搭建Hexo博客及NexT主题配置优化
- 简单的面包屑数据
- java 二、八、十、十六进制之间的转换
- JavaScript的各种对象
- word2vec
- 考试科目