codeforces 699D Fix a Tree
来源:互联网 发布:跑跑卡丁车淘宝买号 编辑:程序博客网 时间:2024/06/06 04:41
A tree is an undirected connected graph without cycles.
Let's consider a rooted undirected tree with n vertices, numbered1 through n. There are many ways to represent such a tree. One way is to create an array withn integers p1, p2, ..., pn, where pi denotes a parent of vertexi (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 thatpr = r. A vertexr is a root of the tree.
- For all other n - 1 vertices i, there is an edge between vertex i and vertexpi.
A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, forn = 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.
#include<stdio.h>#include<string.h>#include<ctype.h>#include<iostream>#include<vector>#include<sstream>#include<queue>#include<limits.h>#include<set>#include<math.h>#include<algorithm>using namespace std; int ans=0,noroot=0;int pre[200000+10],a[200000+10],status[200000+10],b[200000+10];int root,n,indexx=0;int find(int x){ int r=x; while(pre[r]!=r) r=pre[r]; int i=x,j; while(i!=r) { j=pre[i]; pre[i]=r; i=j; } return r;}void join(int x,int y){ int fx=find(x),fy=find(y); if(fx!=fy) { pre[fx]=fy; b[indexx++]=y; } else { if(noroot==1) { noroot=0; b[indexx++]=x; root=x; ans++; status[x]=1; } else { status[fx]=1; b[indexx++]=root; } }}int main(void){ cin>>n; memset(status,0,sizeof(status)); for(int i=1;i<=n;i++) { pre[i]=i; } for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } root=-1; for(int i=1;i<=n;i++) { if(a[i]==i) { root=i; break; } } if(root==-1) { noroot=1; } for(int i=1;i<=n;i++) { join(i,a[i]); } for(int i=1;i<=n;i++) { if(status[i]!=0) ans++; } cout<<ans-1<<endl; for(int i=0;i<n;i++) printf("%d ",b[i]); return 0;}
- codeforces 699D Fix a Tree
- codeforces 699D - Fix a Tree 树的特性
- 【27.48%】【codeforces 699D】 Fix a Tree
- Codeforces 699D. Fix a Tree【并查集+思维】
- Fix a Tree CodeForces
- Fix a Tree CodeForces
- 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 (Div. 2)D. Fix a Tree
- Codeforces #363 (Div. 2) D. Fix a Tree
- CodeForces 699D—— Fix a Tree(并查集判断环)
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
- CF363Ddiv2 D Fix a Tree
- CF 699D Fix a Tree 并查集
- CF#699 D. Fix a Tree (并查集)
- Java多线程应用实例
- Android核心组件笔记 待整理
- 8. String to Integer (atoi)
- Android 程序员必须掌握的三种自动化测试方法
- java链接生成二维码-改编
- codeforces 699D Fix a Tree
- MongoDB 3.2.7安装及用户角色配置
- TCP连接复用
- 汤晓鸥:百度并没有“作弊” 吴韧工作令人钦佩
- 联想G40 U盘装系统问题解决
- 使用两个栈实现一个队列
- lintcode balanced-binary-tree 平衡二叉树
- Maven+SpringMVC3+MyBatis+FreeMarker
- android基础(四)