D. 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.
题意:用一个数组可以表示一棵树,其中Ai表示i的father,根节点的father是本身。但不是所有数组都表示一棵树,现在给你一个数组,用最少的修改次数将之改成一棵树,并输出修改次数和修改后的数组。
做法:用并查集,扫一遍,每次将自己和父亲节点合并。过程中,假如父亲节点的根节点是本身,表示出现了环,标记tag=1,假如父亲节点是本身,表示是根节点,标记tag=0;假如有非环根节点,那将所有其他独立根节点和环都连到这个根节点上,ans=cnt0+cnt1-1;假如全是环,将一个环上的点置为根节点,其他的环连到这个根节点上,ans=cnt1;
zz处:第一发提交WA在把一个fst==-1写成了fst=-1,改之,pp了。然而fst=-1有两处我只改了一处,所以真的fst了,QAQ。之后改了秒过,唉。以后要注意查到错时看看是否有相同的错,全部改了。
然后我很好的一次过4题的机会就这样GG辣,桑心,还掉了分。
代码:
#include <bits/stdc++.h>
using namespace std;
int rec[200500];
int u[200500];
int tag[200500];
bool vis[200500];
int n;
int find(int x)
{
if(x==u[x])return x;
else return u[x]=find(u[x]);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&rec[i]);
u[i]=i;
tag[i]=0;
}
for(int i=1;i<=n;i++)
{
if(i==rec[i])continue;
int tmp=find(rec[i]);
if(tmp==i){
tag[i]=1;
rec[i]=i;
}
else{
u[i]=tmp;
}
}
int cnt0=0;
int cnt1=0;
int fst=-1;
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
{
int tmp=find(i);
if(!vis[tmp]){
vis[tmp]=true;
if(fst==-1&&tag[tmp]==0)
{
fst=tmp;
break;
}
}
}
memset(vis,false,sizeof(vis));
int ans;
if(fst==-1)
{
for(int i=1;i<=n;i++)
{
int tmp=find(i);
if(!vis[tmp]){
vis[tmp]=true;
if(fst==-1)
{
fst=tmp;
cnt1++;
}
else{
rec[tmp]=fst;
cnt1++;
}
}
}
ans=cnt1;
}
else{
for(int i=1;i<=n;i++)
{
int tmp=find(i);
if(!vis[tmp]){
vis[tmp]=true;
if(tag[tmp]==0)
{
cnt0++;
rec[tmp]=fst;
}
else {
cnt1++;
rec[tmp]=fst;
}
}
}
ans=cnt0+cnt1-1;
}
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
printf("%d%c",rec[i],i==n?'\n':' ');
}
}
return 0;
}