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.

【题解】



题意:

要把多个子图、可能带环的图合并成一颗树;只有n条边.

做法:

先确定树的根节点是什么;

如果一开始给的数据里面没有fa[i]==i的情况。

那么就在各个子图里面的环里面找一个根节点(因为没有fa[i]==i,则必然是所有的子图都存在环),随便找一个就可以了。

然后把各个子图的环中的任意一个节点改一下,接到根节点上就可以了;

链怎么办?如果是链那么肯定会有fa[i]==i这样的数据的;

比如2 3 4 4

最后的fa[4]==4,这样才是一条链。

那么我们访问到4的时候,再访问fa[4]就又为4了。则把fa[4]改成树的根节点(我们的程序会认为这是一个环).一切都顺理成章~~

提供两张图供理解



#include <cstdio>
#include <iostream>
#include <cstring> using namespace std; const int MAXN = 2e5 + 100; int n, fa[MAXN],root = 0,vis[MAXN],cnt = 0,ans = 0;
bool rooted = false; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
} void dfs(int x)
{
vis[x] = cnt;
if (vis[fa[x]] == cnt) {//出现了环
if (!rooted) {//如果之前还没找到一个根节点,那么这个节点作为根节点
rooted = true;
root = x;
}
fa[x] = root;
ans++;
return;
}
else
if (vis[fa[x]] != -1)
return;
dfs(fa[x]);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
memset(vis, 255, sizeof(vis));
input(n);
for (int i = 1; i <= n; i++){
input(fa[i]);
if (fa[i] == i && !rooted){
rooted = true;
vis[i] = ++cnt;
root = i;
}
}
for (int i = 1; i <= n; i++)
if (vis[i] == -1) {
cnt++;
dfs(i);
}
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
printf("%d%c", fa[i], (i == n ?'\n':' '));
return 0;
}

【27.48%】【codeforces 699D】 Fix a Tree的更多相关文章

  1. 【CodeForces 699D】Fix a Tree

    dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. Codeforces Round #363 Fix a Tree(树 拓扑排序)

    先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...

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

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

  5. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【27.66%】【codeforces 592D】Super M

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【27.40%】【codeforces 599D】Spongebob and Squares

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 硬件——STM32 , 录音,wav

    详细的wav头文件解析,有例子:http://www.cnblogs.com/chulin/p/8918957.html 关于录音程序的编写: 我的思路是改写原子的程序,原子的程序需要借助VS1053 ...

  2. DIV+CSS学习笔记

    第十五章 定位 static静态定位(不对它的位置进行改变,在哪里就在那里) 默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明 ...

  3. APP测试10点

    1.安装和卸载●应用是否可以在IOS不同系统版本或android不同系统版本上安装(有的系统版本过低,应用不能适配)●软件安装后是否可以正常运行,安装后的文件夹及文件是否可以写到指定的目录里.●安装过 ...

  4. ex.Message "ORA-01691: Lob 段 USER_MURPHY.SYS_LOB0000093717C00006$$ 无法通过 1024 (在表空间 ZJHH 中) 扩展"

    Oracle,往数据库里导入dmp的时候报错:ORA-01691:Lob 段 无法通过8192(在表空间TS_SI中)扩展 解决方案1: 原因:所创建的表空间不足. 创建一个可拓展的表空间 creat ...

  5. 关于Altium Designer的BOM,元件清单

    在生成BOM列表的时候,要记得调整BOM的表格的宽度,以免显示不全, 还有就是BOM列表一共有 comment栏 ,description栏,designator栏,footprint栏,libref ...

  6. es6三点运算符的用法

    扩展运算符( spread )是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1,2,3]); console.log(1,. ...

  7. Android 图片压缩,基于比例和质量压缩

    package cc.util.android.image; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...

  8. URAL 1542. Autocompletion 字典树

    给你最多10w个单词和相应的频率 接下来最多1w5千次询问 每次输入一个字符串让你从前面的单词中依照频率从大到小输出最多10个以该字符串为前缀的单词 開始把单词建成了字典树 然后每次询问找到全部满足条 ...

  9. Spring之i18n配置与使用

    Spring的i18n配置: <!-- conf:i18n --> <bean id="messageSource" class="org.spring ...

  10. Android 开发--CMakeList调用本地so文件

    这里写代码片Android开发常常遇到Java调用so文件的情况,本文介绍一下Google最近新推出的应用在android studio中的方法–cmakelist.txt格式调用. so文件分为jn ...