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

185 篇文章 0 订阅
50 篇文章 0 订阅

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 (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]<<" ";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值