CF698B.Fix a Tree
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 _p_1, _p_2, ..., p__n, where p__i 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 _p_1, _p_2, ..., p__n, one is able to restore a tree:
- There must be exactly one index r that p__r = 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 p__i.
A sequence _p_1, _p_2, ..., p__n 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 _a_1, _a_2, ..., a__n, 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.
树是一类无向连通且无环的图。
考虑一棵以某顶点为根的无向树,其包含 n 个顶点,编号为 1 到 n。表示这样一颗树的方法有很多。其中一种方法是构造一个长度为 n 的整数数组 p1,p2,…,pn,其中 pi 表示顶点 i 的父节点(为方便起见,规定根节点的父节点为其自身)。
对于该有根树,对应的数组 p 为 [2,3,3,2]。
给定一个序列 p1,p2,…,pn,我们可以据此还原出一棵树:
- 必须恰好存在一个下标 r,使得 pr=r。顶点 r 即为该树的根;
- 对其余 n−1 个顶点 i,均存在一条连接顶点 i 与顶点 pi 的边。
若按上述过程可生成某棵(任意)有根树,则称序列 p1,p2,…,pn 是合法的。例如,当 n=3 时,序列 (1,2,2)、(2,3,1) 和 (2,1,3) 均不合法。
现给定一个序列 a1,a2,…,an,它未必合法。你的任务是通过修改最少数量的元素,使其变为合法序列。请输出所需的最少修改次数,以及一次修改后得到的合法序列。若存在多种方案均可达到最少修改次数,请输出其中任意一种即可。
输入格式
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 _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ n).
输入的第一行包含一个整数 n(2≤n≤200000)——树中顶点的数量。
第二行包含 n 个整数 a1,a2,...,an(1≤ai≤n)。
输出格式
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 (_a_1, _a_2, ..., a__n) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
第一行输出得到有效序列所需的最少修改元素个数。
第二行输出一个由 (a1,a2,...,an) 经过最少次数修改后得到的有效序列。若存在多个满足条件的序列,输出任意一个即可。
输入输出样例
输入#1
4 2 3 3 4
输出#1
1 2 3 4 4
输入#2
5 3 2 2 5 3
输出#2
0 3 2 2 5 3
输入#3
8 2 3 5 4 1 6 6 7
输出#3
2 2 3 7 8 1 6 6 7
说明/提示
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 _p_4 = 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.
在第一个样例中,只需修改一个元素即可。在所提供的输出中,序列表示一棵以顶点 4 为根的树(因为 p4=4),如下图左侧所示。另一个正确的解法是序列 2 3 3 2,它表示一棵以顶点 3 为根的树(如下图右侧所示)。在两张图中,根节点均以红色标出。

在第二个样例中,给定的序列本身已是合法的。
输入解题思路,AI测评打分。不知道怎么写?