Codeforces Round 363 (Div. 2)
D
Fix a Tree
Codeforces
未评级
原题
← CE →
翻译中…

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).

翻译中…

image 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:

翻译中…
  1. There must be exactly one index $*r*$ that $*p*_{*r*} = *r*$. A vertex $*r*$ is a root of the tree.
  2. 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.

翻译中…

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 $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($1 ≤ *a*_{*i*} ≤ *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 $(*a*_{1}, *a*_{2}, ..., *a*_{*n*})$ in the minimum number of changes. If there are many such sequences, any of them will be accepted.

翻译中…

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 $*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.

翻译中…

image

翻译中…

In the second sample, the given sequence is already valid.

翻译中…

样例

Input #1
4
2 3 3 4
Output #1
1
2 3 4 4
InputCopy OutputCopy
Input #2
5
3 2 2 5 3
Output #2
0
3 2 2 5 3
InputCopy OutputCopy
Input #3
8
2 3 5 4 1 6 6 7
Output #3
2
2 3 7 8 1 6 6 7
InputCopy OutputCopy
题目信息
题号
699D
时间限制
2 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。