WCWBTT
Time limit20sMemory limit1024 MB
Maintain a rooted tree under parent changes and output the vertex set on each path query using set-union operations under strict cost limits.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Implementation
- Solved
- No attempts yet
Problem
This is an interactive problem.
You need to maintain a rooted tree with vertex as the root. The tree has vertices, and the parent of vertex () is ().
You have to process queries, each in one of the following forms:
?: Output the set of all vertices on the unique simple path from to .=: Change the parent of to (). The vertices are guaranteed to still form a tree after the change, but is not guaranteed.
The set can be too large to print every element in each query.
To deal with this, a special computer maintains sets of integers and operates on them quickly. Initially the computer has only sets , where and for .
The computer supports only two operations.
+: Construct a new set with . The new ID is one more than the maximum ID of all sets. The cost is .!: Print as the answer. You must ensure for all . The cost is .
The following limits apply.
- The cost of each operation cannot exceed .
- The total cost of all operations cannot exceed .
- The total number of operations cannot exceed .
Input
The first line contains two integers and (, ).
The next line contains integers (), the initial parent of each vertex.
Hint
The sample input and output only illustrate the interaction protocol. The blank lines and flush markers are there for readability, and you should not print them.

The figure corresponds to the sample test case.