您的位置:首页 > 其它

CodeForces 698B - Fix a Tree(并查集)

2016-11-08 21:03 513 查看
B. Fix a Tree

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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

题意:

给出一组数列,p[i]是i的父节点,表示一个图.要求用最小的步骤改变一些节点的父节点,使得整个图变成一个树.

解题思路:

遇到的两重情况:1.有环 2.有多棵树

其实总的概括,就是遍历到的当前节点的祖先就是他自身.

所以要把这些特殊节点放到一个容器中,最终将他们挂到根节点的下面.

在找根节点的时候,如果遇到p[i] == i,就让i成为根节点,要么就从容器里面随便找出一个节点,当做根节点.

如果之前已经找到了,那么你所操作的次数就是容器的长度,否则就是容器长度减一(因为把里面的一个当做根节点,那么他就不用进行操作)

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int p[maxn],fa[maxn];
vector<int>ve;
int Find(int x) {return fa[x]?Find(fa[x]):x;}
int main()
{
int n;
scanf("%d",&n);
int root = -1;
for(int i = 1;i <= n;i++)
{
scanf("%d",&p[i]);
if(p[i] == i)   root = i;
if(i == Find(p[i])) ve.push_back(i);
else    fa[i] = p[i];
}
int res = (root == -1)?ve.back():root;
for(int i = 0;i < ve.size();i++)    p[ve[i]] = res;
printf("%d\n",ve.size()-(root != -1));
for(int i = 1;i <= n;i++)
{
if(i == 1)  printf("%d",p[i]);
else        printf(" %d",p[i]);
}
putchar('\n');
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: