#P9090. 「SvR-2」G64
「SvR-2」G64
Problem Description
For two rooted binary trees , define the result of as a binary tree whose root’s left subtree is and right subtree is . Obviously, the result of is unique and always exists.
For a rooted binary tree , define a function . Here, means: starting from the root of , keep moving to the right child until reaching a node that has no right child; then set that node’s right subtree to be . The resulting new tree is . For , satisfies:
$$G_x(T)=G_1(\operatorname{merge}(G_{x-1}(T),G_{x-1}(T)))$$Given a rooted binary tree with nodes and root , let the subtree rooted at node be . There are queries. Each query gives . For each query, find the maximum independent set of .
Input Format
The first line contains two integers .
Then follow lines. Each line contains two integers , representing the left child and right child. If it is , it means the corresponding child does not exist.
Then follow lines. Each line contains two integers , representing one query.
Output Format
Output lines. Each line contains one number: the size of the maximum independent set of modulo for this query.
5 3
2 3
0 4
5 0
0 0
0 0
2 5
2 1
1 1
5
24
6
5 1
2 3
0 4
5 0
0 0
0 0
64 1
592424678
Hint
Sample Explanation
For the first sample, the result of is shown in the figure below (node indices are ignored):

For the second sample, I have a wonderful explanation, but unfortunately is too large to write it down here.
Constraints and Notes
This problem enables bundled testdata and O2 optimization.
| Subtask | Constraints / Special Properties | Score |
|---|---|---|
| The size of is guaranteed to be | ||
| No special restrictions |
For of the testdata: , , .
Translated by ChatGPT 5