#P10610. 异界之门
异界之门
Description
With keen intuition, Renko realized the method to enter this otherworld must be linked to these special Jizō statues. She discovered they formed a uniquely structured tree.
Given a rooted tree with nodes, where the root is node , and each node has a weight . For any two nodes at the same depth, they have the same number of children.
To enter the otherworld, Renko performed operations to modify the tree's node weights:
- Select an edge connecting nodes , where is the deeper node (i.e., is 's child). Add to .
- This operation can be performed any number of times, but the same edge cannot be selected more than once.
After these operations, Renko derived a DFS sequence of the tree and recorded the corresponding weight sequence (specifically, is the weight of the -th node visited in the DFS order).
Unfortunately, she forgot which operations she performed and how the DFS was conducted. Your task is to reconstruct any valid set of operations and DFS sequence.
Input Format
- The first line contains an integer .
- The next lines each contain two integers and :
- is the parent of node (note: , and for other nodes, ).
- is the initial weight of node .
- The next line contains integers describing the target weight sequence . It is guaranteed that there exists a valid set of operations and DFS order to achieve .
Output Format
- The first line contains an integer , the number of operations performed.
- The second line contains integers , where represents the edge between node and its parent chosen in the -th operation.
- The third line contains a permutation of integers, where is the -th node visited in your constructed DFS sequence.
4
0 1
1 2
2 3
3 4
1 3 5 9
3
3 4 2
1 2 3 4
5
0 1
1 -1
1 -1
2 3
3 4
1 0 3 0 3
3
2 5 3
1 2 4 3 5
4
0 1
1 2
1 3
1 4
1 4 3 3
1
2
1 4 2 3
5
0 1
1 1
1 1
2 1
3 1
1 2 2 2 3
4
2 4 5 3
1 3 5 2 4
Hint
Sample Explanations
Sample #1
One valid solution is to operate edges , , in order. The final weights are , and the DFS sequence is .
This sample satisfies Special Property A.
Sample #2
One valid solution is to operate edges , , . The final weights are , and the DFS sequence is .
Sample #3
One valid solution is to operate edge . The final weights are , and the DFS sequence is .
This sample satisfies Special Property B.
Sample #4
One valid solution is to operate edges , , , . The final weights are , and the DFS sequence is .
This sample satisfies Special Property C.
Constraints
Bundled testing is used.
$$\def\arraystretch{1.5} \begin{array}{|c|c|c|c|c|}\hline \textbf{Subtask} & \textbf{Points} & \bm{n \leq} & \textbf{Special Property} & \textbf{Subtask Dependencies} \cr\hline 1 & 10 & 6 & - & - \cr\hline 2 & 10 & 100 & \mathbf{A} & - \cr\hline 3 & 10 & 100 & \mathbf{B} & - \cr\hline 4 & 15 & 2 \times 10^3 & \mathbf{C} & - \cr\hline 5 & 15 & 2 \times 10^3 & \mathbf{D} & - \cr\hline 6 & 15 & 100 & - & 1,2,3 \cr\hline 7 & 25 & 2 \times 10^3 & - & 1,2,3,4,5,6 \cr\hline \end{array}$$Special Properties:
- A: The tree satisfies for .
- B: The tree satisfies for .
- C: All nodes have .
- D: All non-leaf nodes have at most 2 children.
For all data:
, , .
Translated by DeepSeek R1
京公网安备 11011102002149号