Description
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.
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.
Sample Input
4 2 3 3 4
1 2 3 4 4
5 3 2 2 5 3
0 3 2 2 5 3
8 2 3 5 4 1 6 6 7
2 2 3 7 8 1 6 6 7
Hint
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 (becausep4 = 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.
题意:输入一些点的父亲,以最少的改变,将他们修成一棵树
他们可能是 自点(即点),树,环,一定注意这里,环可以是一个环上有几个尾巴
思路:把自己为自己父亲的点存入tree数组,用并查集处理,把确定端点的boos标记,遍历把环找出来,环上一点存入cycle数组,最后每个定点连向第一棵树的顶点,(这个想法真心烂啊,我还是太菜了...........)
#include<stdio.h>
#include<string.h>
int a[200009];
bool book[200009],book2[200009];
int f[200009];
int tree[200009];
int cycle[200009];
int find(int x){
return x==f[x]?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
f[find(x)]=find(y);
}
int main(){
int i,j,t;
int k;
int t1,t2;
scanf("%d",&t);
for(i=1;i<=t;i++)
f[i]=i;
t1=0;
t2=0;
memset(book,false,sizeof(book));
for(i=1;i<=t;i++){
scanf("%d",a+i);
merge(i,a[i]);
if(a[i]==i) tree[t1++]=i;
}
for(i=0;i<t1;i++){
book[f[tree[i]]]=true;
}
// for(i=1;i<=t;i++)
// printf("%d\n",f[i]);
for(i=1;i<=t;i++){
find(i);
if(book[f[i]])continue;
book[f[i]]=true; //本集合被标记
j=i;
memset(book2,false,sizeof(book2));
while(1){
if(book2[j]) {//遍历找到一个环,cycle
cycle[t2++]=j;
break;}
book2[j]=true;
j=a[j];
}
}
if(t1==0){//全都是环
printf("%d\n",t2);
for(i=0;i<t2;i++)
a[cycle[i]]=cycle[0];
}
else {
printf("%d\n",t1+t2-1);
for(i=1;i<t1;i++)
a[tree[i]]=a[tree[0]];
for(i=0;i<t2;i++)
a[cycle[i]]=a[tree[0]];
}
for(i=1;i<=t;i++)
printf("%d ",a[i]);
return 0;
}
下面这代码是一大神的,膜拜,最后的输出有学到啊。再说一句,我好弱啊,
#include<cstdio>
#define F(i,a,b) for(int i=a;i<=b;i++)
const int N=2E5+7;
int a[N],fa[N];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
int n,boss=-1,cnt=0;
scanf("%d",&n);
F(i,1,n)fa[i]=i;
F(i,1,n){
scanf("%d",a+i);
if(a[i]==i)boss=i,cnt++;//任意的孤立点或者任意一个集合的顶点都能当最终的顶点
else{
int fx=find(i),fy=find(a[i]);
if(fx==fy){//说明这里存在环
cnt++,a[i]=i;
}else fa[fx]=fy;//将这两点连接
}
}
if(boss==-1){
F(i,1,n)if(fa[i]==i){boss=i;break;}
cnt++;
}
printf("%d\n",cnt-1);//因为多记了一个树的顶点
F(i,1,n){
if(a[i]==i)a[i]=boss;
printf("%d%c",a[i]," \n"[i==n]);
}
return 0;
}