#P96. [NOI 2025 Day 1] 数字树

[NOI 2025 Day 1] 数字树

Person in Charge

Problem Description

Given a binary tree with 4n14n - 1 nodes, where each non-leaf node has exactly two children. The non-leaf nodes are numbered from 11 to 2n12n - 1, and the leaf nodes are numbered from 2n2n to 4n14n - 1. 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 44 and 66 are labeled with 11, and leaf nodes 55 and 77 are labeled with 22, then the sequence formed by the DFS traversal [1,4,2,7,3,5,6][1, 4, 2, 7, 3, 5, 6] is [1,2,2,1][1, 2, 2, 1]. This sequence can be reduced to [1,1][1, 1] by eliminating the adjacent 22s, and further to an empty sequence by eliminating the adjacent 11s, so this DFS traversal is elegant. On the other hand, the DFS traversal [1,4,2,3,5,6,7][1, 4, 2, 3, 5, 6, 7] produces the sequence [1,2,1,2][1, 2, 1, 2], which cannot be reduced to an empty sequence by eliminating adjacent identical numbers, so this DFS traversal is not elegant.

Given nn operations, the ii-th (1in1 \le i \le n) operation selects two unlabeled leaf nodes and labels them with the number ii. 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 1,000,000,0071,000,000,007.

Input Format

The first line of input contains a non-negative integer cc, representing the test case number. c=0c = 0 indicates that the test case is a sample.

The second line of input contains a positive integer nn, indicating that the binary tree has 4n14n - 1 nodes.

The (i+2)(i + 2)-th line (1i2n11 \le i \le 2n - 1) contains two positive integers lil_i and rir_i, representing the left and right children of node ii, respectively. It is guaranteed that i<li,ri4n1i < l_i, r_i \le 4n - 1, and all lil_i and rir_i are distinct.

The (i+2n1)(i + 2n - 1)-th line (1in1 \le i \le n) contains two positive integers aia_i and bib_i, representing the leaf nodes selected in the ii-th operation. It is guaranteed that 2nai,bi4n12n \le a_i, b_i \le 4n - 1, and all aia_i and bib_i are distinct.

Output Format

Output nn lines, where the ii-th line (1in1 \le i \le n) contains a non-negative integer representing the number of elegant DFS traversals after the ii-th operation, modulo 1,000,000,0071,000,000,007.

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 44 and 66 are labeled with 11, and leaf nodes 55 and 77 are unlabeled. The sequence formed by any DFS traversal is [1,1][1, 1], so all 23=82^3 = 8 DFS traversals are elegant.
  • After the second operation, leaf nodes 474 \sim 7 are labeled with 1,2,1,21, 2, 1, 2, respectively. There are 44 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 6106 \sim 10.

Sample 3

See the files tree/tree3.in and tree/tree3.ans in the contestant directory.

This sample satisfies the constraints of test cases 11,1211, 12.

Sample 4

See the files tree/tree4.in and tree/tree4.ans in the contestant directory.

This sample satisfies the constraints of test cases 172017 \sim 20.

Sample 5

See the files tree/tree5.in and tree/tree5.ans in the contestant directory.

This sample satisfies the constraints of test cases 24,2524, 25.

Data Range

For all test data, the following guarantees hold:

  • 1n2×1051 \le n \le 2 \times 10^5;
  • For all 1i2n11 \le i \le 2n - 1, i<li,ri4n1i < l_i, r_i \le 4n - 1, and all lil_i and rir_i are distinct;
  • For all 1in1 \le i \le n, 2nai,bi4n12n \le a_i, b_i \le 4n - 1, and all aia_i and bib_i are distinct;
  • After each operation, there exists at least one elegant DFS traversal.
Test Case nn \le Special Properties
1,21, 2 1010 None
353 \sim 5 10210^2 A
6106 \sim 10 None
11,1211, 12 10310^3 A
13,1413, 14 None
15,1615, 16 5×1045 \times 10^4 AB
172017 \sim 20 B
21,2221, 22 None
2323 2×1052 \times 10^5 A
24,2524, 25 None
  • Special Property A: The two leaf nodes selected in each operation are in different subtrees of node 11.
  • Special Property B: There exists a non-negative integer mm such that n=2mn = 2^m, and for all 1i2n11 \le i \le 2n - 1, li=2il_i = 2i and ri=2i+1r_i = 2i + 1.