Codeforces Round #363 (Div. 2)D. Fix a Tree(並查集)

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:

  1. There must be exactly one index r that pr = r. A vertex r is a root of the tree.
  2. 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 (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.

題意:

給你n個頂點,n個數代表第i這個頂點連接的ai這個頂點,如果i=ai表示他沒連向其他頂點,不過其他頂點可能連向他,然後問你至少改變多少個ai使得這n個頂點能變成一棵樹

題解:

要最少的改變,我們分析一下,首先要成爲一棵樹,不能有孤立點,不能有環,所以我們只需要改變孤立點的連向和會使樹變成環的點就行了,

我們這裏用並查集維護連接的關係,任何孤立點都能當作樹的頂點,如果有一個點會使其成爲環,那麼我們就暫時將這個點的當作這一棵樹的頂點,

爲什麼是暫時呢?因爲這一棵樹有可能只是一棵小樹,我們要合併全部的樹,所以暫時當作這顆樹的頂點,最後合併的時候就直接指向大樹的頂點就行了,這樣就能達到最小的改變,如果給的n個點形成的兩棵子樹,那麼我們只需要任選一個樹的頂點作爲boss,然後改變另一棵樹的頂點就能合併了。再總結一下,我們只改變了孤立點和會使其成爲環的點然後合併子樹的時候也只是改變了頂點的指向,這些都是必須要改變的,所以最後的答案肯定是最小的

#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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章