codeforces 699D Fix a Tree

来源:互联网 发布:跑跑卡丁车淘宝买号 编辑:程序博客网 时间:2024/06/06 04:41
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, numbered1 through n. There are many ways to represent such a tree. One way is to create an array withn integers p1, p2, ..., pn, where pi denotes a parent of vertexi (here, for convenience a root is considered its own parent).

For this rooted tree the arrayp 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 thatpr = r. A vertexr is a root of the tree.
  2. For all other n - 1 vertices i, there is an edge between vertex i and vertexpi.

A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, forn = 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.


题目大意就是给一组父节点数列,问能不能构成一棵树。不能的话,输出最小改动次数,并输出改动后的父节点数列。
首先树有什么要求呢?首先,有且只有一个父节点;其次,不能有环。
判断环可以用并查集,没有父节点的情况要好好想想,我一开始就随便把1的父节点设成1,结果错了,因为这不是最优解。应该去找一对出了问题的节点,把其中一个改成父节点,这样答案可以少一。

#include<stdio.h>#include<string.h>#include<ctype.h>#include<iostream>#include<vector>#include<sstream>#include<queue>#include<limits.h>#include<set>#include<math.h>#include<algorithm>using namespace std; int ans=0,noroot=0;int pre[200000+10],a[200000+10],status[200000+10],b[200000+10];int root,n,indexx=0;int find(int x){    int r=x;    while(pre[r]!=r)        r=pre[r];    int i=x,j;    while(i!=r)    {        j=pre[i];        pre[i]=r;        i=j;    }    return r;}void join(int x,int y){    int fx=find(x),fy=find(y);    if(fx!=fy)    {        pre[fx]=fy;        b[indexx++]=y;    }    else    {        if(noroot==1)        {            noroot=0;            b[indexx++]=x;            root=x;            ans++;            status[x]=1;        }        else        {            status[fx]=1;            b[indexx++]=root;        }    }}int main(void){    cin>>n;    memset(status,0,sizeof(status));    for(int i=1;i<=n;i++)    {        pre[i]=i;    }    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i]);    }    root=-1;    for(int i=1;i<=n;i++)    {        if(a[i]==i)        {            root=i;            break;        }    }    if(root==-1)    {        noroot=1;    }    for(int i=1;i<=n;i++)    {        join(i,a[i]);    }    for(int i=1;i<=n;i++)    {        if(status[i]!=0)            ans++;    }    cout<<ans-1<<endl;    for(int i=0;i<n;i++)        printf("%d ",b[i]);    return 0;}

0 0
原创粉丝点击