Codeforces Round #363 D Fix a Tree(并查集)
来源:互联网 发布:php popen 编辑:程序博客网 时间:2024/05/16 19:29
D. Fix a Tree
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).
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.
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).
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.
42 3 3 4
12 3 4 4
53 2 2 5 3
03 2 2 5 3
82 3 5 4 1 6 6 7
22 3 7 8 1 6 6 7
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;}
- Codeforces Round #363 (Div. 2) D. Fix a Tree(并查集)(判断圆环)
- Codeforces Round #363 (Div. 2) Problem D - Fix a Tree(并查集 + 构造)
- Codeforces Round #363 D Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2) D. Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2) D Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2) D. Fix a Tree (并查集)
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
- Codeforces Round #363 (Div. 2)D. Fix a Tree并查集
- (环问题并查集)Codeforces Round #363 D Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
- Codeforces 699D. Fix a Tree【并查集+思维】
- CF#699 D. Fix a Tree (并查集)
- codeforces #363 fix a tree 并查集
- Codeforces 698C. Fix a Tree (并查集)
- CodeForces 699D—— Fix a Tree(并查集判断环)
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Codeforces Round #363 (Div. 2) D. Fix a Tree
- Special LATEX symbols
- 关于 chmod
- clipchildren 咸鱼app 中间 图标很大的 底部 tab栏 实现方案
- 调试CAN程序时遇到的几个问题
- 三态门的概念
- Codeforces Round #363 D Fix a Tree(并查集)
- php基础要点总结
- 黑马程序员_Map集合中按value值进行排序
- Biker's Trip Odometer(求平均速度)
- 第 45课:神速Spark的新解析引擎catalyst
- listview、scollview、webview的下拉刷新,上拉加载。效果特别好,带刷新时间和图片
- __bridge,__bridge_retained和__bridge_transfer的意思,区别与使用
- 内存管理
- python运行不了 AttributeError: module object has no attribute getdefaultlocale