Generalized Insertion Sort
Time limit2sMemory limit256 MB
Given a rooted tree with a permutation on its vertices, sort the values using at most 25,000 path-rotation operations from the root to a chosen vertex.
- Level
Hard8 of 10
- Topics
- Tree, Simulation, Sorting
- Solved
- No attempts yet
Problem
You are given a rooted tree with vertices, numbered . The root is vertex , and the parent of vertex () is vertex .
Initially, vertex holds the integer . Here, is a permutation of .
You can perform the following operation at most times. The goal is to make the value at vertex equal to for every .
- Choose a vertex . Consider the path from vertex to .
- Rotate the values on this path. For each edge on the path, the value at becomes the value at (before this operation), and the value at becomes the value at (before this operation).
- Choosing vertex does nothing.
Input
Input is given in the following format:
...
...
Output
In the first line, print the number of operations . In the next lines, print the chosen vertices in order.
Constraints
, , and is a permutation of .
Hint
In Sample 1, after the first operation, the values at vertices are .
In Sample 2, after the first operation, the values at vertices are . After the second operation, they are .