Fix a Tree

time limit per test2 seconds

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.

大概意思是:
给出 n 个结点的父亲,问至少修改多少个结点的父亲,能使整张图变成
一棵树(根的父亲为自己),要求输出任一方案。
其中 1 ≤ n ≤ 200000。

我的想法是你先按最小生成树的方法生成树,然后找一个点为根节点,有现成的就用,没有就选一个。
然后把每棵树都连起来就成了一棵新的树了。。。

写完以后很开心的发现1A了。。然后去看了看题解。。。

别人的方法是:
思考环和链的答案
图的各个弱连通块是环 + 内向树,或者树/环。
先用拓扑排序把内向树消掉
剩下来的是一些环,每个环随便选一个结点当根,然后再把所有的根连
在一起。
答案是环数-(是否存在自环)

其实我也没怎么看懂,我觉得道理应该差不多吧
```c++

include<bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;

int n, root, tot, fa[maxn], ini[maxn];

bool flag[maxn];

set s;

set::iterator iter;

inline int read()

{

int s = 0, w = 1; char ch = getchar();

while(ch <= '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}

while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();

return s * w;

}

int find(int t){return t == fa[t] ? t : fa[t] = find(fa[t]);}

int main()

{

n = read();

for(int i = 1; i <= n; ++i) ini[i] = read(), fa[i] = i;

for(int A, B, i = 1; i <= n; ++i){

A = find(i); B = find(ini[i]); if(A == B) continue;

fa[A] = B; flag[i] = true;

}

for(int i = 1; i <= n; ++i){s.insert(find(fa[i])); if(i == ini[i]) root = i;}

for(iter = s.begin(); iter != s.end(); ++iter) tot++;

if(!root){

for(int i = 1; i <= n; ++i){

if(flag[i]) continue;

root = i; tot++; flag[i] = true; ini[i] = i; break;

}

}

printf("%d\n", tot - 1);

for(int i = 1; i <= n; ++i){

if(flag[i]) printf("%d ", ini[i]);

else printf("%d ", root);

}

return 0;

}

Codeforces Fix a Tree的更多相关文章

  1. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

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

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

  3. Codeforces Round #363 (Div. 2) 698B Fix a Tree

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes     A tree is an und ...

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

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Fix a Tree

    Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...

  6. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  7. 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...

  8. 【codeforces 698B】 Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...

  9. Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环

    题目链接:http://codeforces.com/problemset/problem/698/B题意:告诉你n个节点当前的父节点,修改最少的点的父节点使之变成一棵有根树.思路:拆环.题解:htt ...

随机推荐

  1. react native 之 Android studio

    https\://services.gradle.org/distributions/gradle-4.4-all.zip 下载失败 https://blog.csdn.net/u013132758/ ...

  2. JAVA- 内部类及匿名内部类

    普通类,我们平时见到的那种类,就是一个后缀为.java的文件中,直接定义的类,比如 public Cat{ private String name; private int age; } 内部类, 内 ...

  3. sass-RGB颜色函数-RGBA()函数

    rgba() 函数主要用来将一个颜色根据透明度转换成 rgba 颜色. 其语法有两种格式: rgba($red,$green,$blue,$alpha) //将一个rgba颜色转译出来,和未转译的值一 ...

  4. 前端每日实战:56# 视频演示如何用纯 CSS 描述程序员的生活

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/YvYVvY 可交互视频 此视频是可 ...

  5. Python 把字符串变成浮点数

    from functools import reducedi = {}di.update(zip('1234567890.', [1,2,3,4,5,6,7,8,9,0,'.'])) def str2 ...

  6. spark数据分析导论

    1.spark的定义 spark是一个用来实现快速而通用的集群计算平台,高效的支持更多计算模式,包括交互式查询和流处理. 主要特点就是能够在内存中进行计算,即使在磁盘上进行计算依然比mapreduce ...

  7. mybatis的Date类型。

    在写select的时候,里面的查询语句.where后面如果jdbcType=DATE没有写的话是 这个形式的. <select id="selectPhoto" parame ...

  8. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  9. zoj3229 Shoot the Bullet (有源汇最大流)

    题目大意:文文要给幻想乡的女♂孩子们拍照,一共n天,m个女♂孩子,每天文文至多拍D[i]张照片,每个女♂孩子总共要被文文至少拍G[i]次.在第i天,文文可以拍c[i]个女♂孩子,c[i]个女♂孩子中每 ...

  10. Codeforces 814C - An impassioned circulation of affection

    原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...