Fix a Tree
Contest: Codeforces Round 363 (Div. 1)
Difficulty rating: 1700
Tags: constructive algorithms, dfs and similar, dsu, graphs, trees

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:

1. There must be exactly one index r that pr = 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 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 format:
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 format:
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.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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.

Example input:
4
2 3 3 4

Example output:
1
2 3 4 4

Example input:
5
3 2 2 5 3

Example output:
0
3 2 2 5 3

Editorial:
One can easily see that given sequence describes a functional graph, thus a directed graph with edges going from i to ai for every i. This graph represents a set of cycles and each cycle vertex is a root of its own tree (possibly consisting of one vertex).
Picture above shows an example of a functional graph. It consists of two cycles 1, 6, 3 and 4. Vertex 6 is the root of the tree consisting of vertices 0, 2, 6 and 8, vertex 3 roots the tree of vertices 3 and 5, vertex 4 — root of tree of vertices 4 and 7 and vertex 1 forms a tree of only one vertex.
In terms of functional graph, our goal is to make the graph consisting of exactly one cycle of exactly one vertex looped to itself.
Operation of change is equivalent to removing some outgoing edge and adding a new one, going to somewhat else vertex. Let's firstly make our graph containing only one cycle. To do so, one can choose any of initially presented cycles and say that it will be the only one. Then one should consider every other cycle, remove any of its in-cycle edges and replace it with an edge going to any of the chosen cycle's vertices. Thus the cycle will be broken and its vertices (along with tree ones) will be connected to the only chosen cycle. One will need to do exactly cycleCount - 1 such operations. Note that the removing of any non-cycle edge does not make sense, because it does not break any cycle.
The next thing is to make the cycle length be equal to 1. That might be already satisfied, if one will choose a cycle of minimal length and this length equals 1. Thus, if the initial graph contains any cycle of length 1, we are done with cycleCount - 1 operations. Otherwise, the cycle contains more than one vertex. It can be fixed with exactly one operation — one just need to break any of in-cycle edges, say from u to au, and add an edge from u to u. The graph will remain consisting of one cycle, but consisting of one self-looped vertex. In that case, we are done with cycleCount operations.
To do all the operations above, one can use DSU structure, or just a series of DFS. Note that there is no need in realisation of edge removing and creating, one just needs to analyze initial graph.