#P8204. [Ynoi2005] tdnmo
[Ynoi2005] tdnmo
Problem Description
Given a rooted tree with vertices numbered , where vertex is the root. Define the directed neighborhood as the set of vertices in the subtree rooted at whose distance to is less than , where , and are integers.
You are given directed neighborhoods . Starting from , you need to reach every given directed neighborhood using no more than operations. The allowed operations are:
- Move from a directed neighborhood to , such that .
- Undo one operation of type 1, i.e., return to the position before the most recent not-yet-undone type 1 operation, and mark that type 1 operation as undone.
- Declare that you have currently reached the directed neighborhood , where your current neighborhood equals .
For operation 1, the cost is the difference between the sizes (number of elements) of the two directed neighborhoods before and after the move. Operations 2 and 3 have no cost. When performing operation 2, there must exist a type 1 operation that has not been undone. Operation 3 must be performed exactly once for each .
You must ensure that the total cost of all operations does not exceed .
Input Format
The first line contains two integers .
The next line contains integers, which are the parents of vertices in order. It is guaranteed that the parent index is smaller than the child index.
The next lines each contain two integers . Line describes the given directed neighborhood.
Output Format
The first line contains an integer , the number of operations you perform.
The next lines describe each operation in order.
Operation 1 is written as .
Operation 2 is written as .
Operation 3 is written as .
8 4
1 1 1 2 2 2 5
2 1
1 1
6 0
1 2
16
1 2 1
3 1
2
1 6 0
3 3
2
1 1 1
1 1 1
3 2
2
1 1 2
1 1 2
3 4
2
2
2
Hint
Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078, SPJ: nalemy.
For of the testdata, , , . All values are integers.
Translated by ChatGPT 5