atcoder#APC001H. Generalized Insertion Sort
Generalized Insertion Sort
Score : points
Problem Statement
You are given a rooted tree with vertices. The vertices are numbered . The root is Vertex , and the parent of Vertex is Vertex .
Initially, an integer is written in Vertex . Here, is a permutation of .
You can execute the following operation at most times. Do it so that the value written in Vertex becomes .
- Choose a vertex and call it . Consider the path connecting Vertex and .
- Rotate the values written on the path. That is, For each edge along the path, replace the value written in Vertex with the value written in Vertex (just before this operation), and replace the value of with the value written in Vertex (just before this operation).
- You may choose Vertex , in which case the operation does nothing.
Constraints
- is a permutation of .
Input
Input is given from Standard Input in the following format:
...
...
Output
In the first line, print the number of operations, . In the second through -th lines, print the chosen vertices in order.
5
0 1 2 3
2 4 0 1 3
2
3
4
- After the first operation, the values written in Vertex are .
5
0 1 2 2
4 3 1 2 0
3
4
3
1
- After the first operation, the values written in Vertex are .
- After the second operation, the values written in Vertex are .