#P96. [NOI 2025 Day 1] 数字树
[NOI 2025 Day 1] 数字树
Person in Charge
Problem Description
Given a binary tree with nodes, where each non-leaf node has exactly two children. The non-leaf nodes are numbered from to , and the leaf nodes are numbered from to . Initially, there are no numbers on any leaf nodes.
A DFS traversal is defined as elegant if and only if, when concatenating the numbers on all labeled leaf nodes in the order of the DFS traversal, the resulting sequence can be reduced to an empty sequence by repeatedly eliminating adjacent identical numbers. For example, in the figure below, if leaf nodes and are labeled with , and leaf nodes and are labeled with , then the sequence formed by the DFS traversal is . This sequence can be reduced to by eliminating the adjacent s, and further to an empty sequence by eliminating the adjacent s, so this DFS traversal is elegant. On the other hand, the DFS traversal produces the sequence , which cannot be reduced to an empty sequence by eliminating adjacent identical numbers, so this DFS traversal is not elegant.

Given operations, the -th () operation selects two unlabeled leaf nodes and labels them with the number . It is guaranteed that after each operation, there exists at least one elegant DFS traversal. You need to determine the number of elegant DFS traversals after each operation. Since the answer may be large, you only need to output the result modulo .
Input Format
The first line of input contains a non-negative integer , representing the test case number. indicates that the test case is a sample.
The second line of input contains a positive integer , indicating that the binary tree has nodes.
The -th line () contains two positive integers and , representing the left and right children of node , respectively. It is guaranteed that , and all and are distinct.
The -th line () contains two positive integers and , representing the leaf nodes selected in the -th operation. It is guaranteed that , and all and are distinct.
Output Format
Output lines, where the -th line () contains a non-negative integer representing the number of elegant DFS traversals after the -th operation, modulo .
Samples
0
2
4 2
3 7
5 6
4 6
5 7
8
4
0
6
2 3
4 21
22 23
5 11
6 8
7 9
12 13
10 18
14 15
16 17
19 20
12 13
14 15
16 19
17 18
20 21
22 23
2048
2048
2048
1024
512
512
Sample 1 Explanation
This sample corresponds to the example in the Problem Description.
- After the first operation, leaf nodes and are labeled with , and leaf nodes and are unlabeled. The sequence formed by any DFS traversal is , so all DFS traversals are elegant.
- After the second operation, leaf nodes are labeled with , respectively. There are elegant DFS traversals: $[1, 4, 2, 3, 6, 5, 7], [1, 4, 2, 7, 3, 5, 6], [1, 2, 3, 6, 5, 7, 4], [1, 2, 7, 3, 5, 6, 4]$.
Sample 2
See the files tree/tree2.in and tree/tree2.ans in the contestant directory.
This sample satisfies the constraints of test cases .
Sample 3
See the files tree/tree3.in and tree/tree3.ans in the contestant directory.
This sample satisfies the constraints of test cases .
Sample 4
See the files tree/tree4.in and tree/tree4.ans in the contestant directory.
This sample satisfies the constraints of test cases .
Sample 5
See the files tree/tree5.in and tree/tree5.ans in the contestant directory.
This sample satisfies the constraints of test cases .
Data Range
For all test data, the following guarantees hold:
- ;
- For all , , and all and are distinct;
- For all , , and all and are distinct;
- After each operation, there exists at least one elegant DFS traversal.
| Test Case | Special Properties | |
|---|---|---|
| None | ||
| A | ||
| None | ||
| A | ||
| None | ||
| AB | ||
| B | ||
| None | ||
| A | ||
| None |
- Special Property A: The two leaf nodes selected in each operation are in different subtrees of node .
- Special Property B: There exists a non-negative integer such that , and for all , and .