Codeforces Round #363 (Div. 2) D DFS

来源:互联网 发布:网络渗透测试工程师 编辑:程序博客网 时间:2024/05/22 06:57




链接:戳这里


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.


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<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;struct edge{    int v,next;}e[4000100];int head[200100];int tot=0;void Add(int u,int v){    e[tot].v=v;    e[tot].next=head[u];    head[u]=tot++;}int vis[200100],cnt=0,ans=0,X=-1,root=-1;int anw[200100];int x[200100];void DFS(int u){    for(int i=head[u];i!=-1;i=e[i].next){        int v=e[i].v;        if(!vis[v]){            vis[v]=cnt;            DFS(v);        } else {            if(vis[v]==cnt){                ans++;                if(X==-1){                    if(root==-1) root=v;                    x[v]=root;                } else {                    x[v]=X;                }            }        }    }}int n;int main(){    scanf("%d",&n);    mst(head,-1);    for(int i=1;i<=n;i++){        scanf("%d",&x[i]);        if(x[i]==i && X==-1) {            X=i;        }        else if(x[i]!=i)            Add(i,x[i]);        else if(x[i]==i && X!=-1){            x[i]=X;            ans++;        }    }    for(int i=1;i<=n;i++){        if(!vis[i]){            vis[i]=++cnt;            DFS(i);        }    }    printf("%d\n",ans);    for(int i=1;i<=n;i++) printf("%d ",x[i]);    printf("\n");    return 0;}



0 0
原创粉丝点击