您的位置:首页 > 其它

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

2016-07-20 10:17 459 查看
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.

题意:给你一个父亲表示法的图,让你变动最少的点把它变成一棵树。

分析:先看有没有自环点,有的话可以考虑把它作为根节点,没有的话就找到第一个联通块上的任意环上点,把它的一条环上边拆掉连成自环作为根,其他联通块上的点做相同处理连到根节点上。

#include <cstdio>
#include <queue>
#include <vector>
#include <cstdio>
#include <utility>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
int n,ans,head,fa[200007],f[200007],tot[200007],jud[200007];
vector <int> a[200007];
int find(int x)
{
if(f[x] == x) return x;
f[x] = find(f[x]);
return f[x];
}
int dfs(int x,int v)
{
jud[x] = v;
if(jud[fa[x]] == v) return x;
return dfs(fa[x],v);
}
int main()
{
bool Flag = false;
cin.sync_with_stdio(false);
cin>>n;
for(int i = 1;i <= n;i++) f[i] = i;
for(int i = 1;i <= n;i++)
{
cin>>fa[i];
if(fa[i] == i)
{
head = i;
Flag = true;
}
if (find(fa[i]) != find(i)) f[find(fa[i])] = find(i);
}
for(int i = 1;i <= n;i++)
{
tot[find(i)]++;
a[find(i)].push_back(i);
}
for(int i = 1;i <= n;i++) if(tot[i]) ans++;
if(ans != 1)
{
if(!Flag)
{
ans++;
int i = 1;
for(;i <= n;i++) if(tot[i]) break;
head = dfs(i,i);
fa[head] = head;
for(int j = i+1;j <= n;j++)
if(tot[j]) fa[dfs(j,j)] = head;
}
else
{
for(int i = 1;i <= n;i++)
if(tot[i] && find(i) != find(head))
{
bool flag = false;
for(int v : a[i])
if(fa[v] == v)
{
fa[v] = head;
flag = true;
}
if(!flag) fa[dfs(i,i)] = head;
}
}
}
else
{
if(!Flag)
{
head = dfs(1,1);
fa[head] = head;
ans++;
}
}
cout<<ans-1<<endl;
for(int i = 1;i <= n;i++) cout<<fa[i]<<" ";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: