#P6776. [NOI2020] 超现实树

[NOI2020] 超现实树

Description

X1\mathbf{X1}: Let me translate the message.

Definition: The trees described in this text are defined inductively: a single node forms a tree; taking a tree as the left (or right) child forms a tree; taking two trees as the left and right children respectively also forms a tree. All structures generated by the above rules in finitely many steps are called trees.

X2\mathbf{X2}: So the “trees” here are non-empty, rooted binary trees with distinguished left and right children.

X1\mathbf{X1}: Exactly. Next, the book defines isomorphism of two trees.

Definition: Two trees TT and TT^{\prime} are called isomorphic, written TTT \equiv T^{\prime}, defined by the following four rules:

  1. Trees consisting of a single node are isomorphic to each other.
  2. If the roots of two trees both have only a left subtree, and their left subtrees are isomorphic, then these two trees are isomorphic.
  3. If the roots of two trees both have only a right subtree, and their right subtrees are isomorphic, then these two trees are isomorphic.
  4. If the roots of two trees both have left and right subtrees, and their left subtrees are isomorphic correspondingly and their right subtrees are isomorphic correspondingly, then these two trees are isomorphic.

Obviously, the isomorphism relation forms an equivalence relation on the set of all trees. For convenience, we regard isomorphic trees as the same tree.

X2\mathbf{X2}: Treating isomorphic trees as the same means the nodes are considered identical. Simply put, two trees are isomorphic if and only if they are the same when nodes are unlabeled and left/right children are distinguished. We say two trees are different if and only if they are not isomorphic.

X1\mathbf{X1}: The book also defines leaves of a tree: same as usual, a leaf is a node with no children.

X2\mathbf{X2}: That matches the definition we know. Well, mathematicians are really a bit wordy... Probably only someone like X3\mathbf{X3} would enjoy this style.

X1\mathbf{X1}: I do not really mind it—compared with experience-based “intuition”, precise definitions and rigorous proofs feel more reassuring. Look, the next definition is not so intuitive.

Definition: We say a tree TT single-step replaces into TT^{\prime} if replacing some leaf node of TT with another tree TT^{\prime \prime} yields a tree isomorphic to TT^{\prime}, written TTT \rightarrow T^{\prime}. We say a tree TT replaces into TT^{\prime}, written TTT \rightarrow^{\star} T^{\prime}, if there exist a natural number n1n \geq 1 and trees T1,T2,,TnT_{1}, T_{2}, \ldots, T_{n} such that $T \equiv T_{1} \rightarrow T_{2} \rightarrow \cdots \rightarrow T_{n} \equiv T^{\prime}$.

X2\mathbf{X2}: Let me think... “Replacement” means deleting a leaf node and putting another tree at that position, like the leaf node “grows” a larger subtree. If a tree can replace into another tree, it means the latter can be obtained through zero, one, or multiple single-step replacements. Oh... I see! For example, any tree can replace into itself; in other words, for a tree TT, we always have TTT \rightarrow^{\star} T^{\prime}. The picture below can help understand the meanings of single-step replacement and replacement.

img

X1\mathbf{X1}: You are right. In particular, any tree can replace into infinitely many different trees, and only the single-node tree can replace into any other tree. The book also defines something like this.

Definition: For a tree TT, define grow(T)\operatorname{grow}(T) as the set of trees that TT can obtain by replacement, i.e. $\operatorname{grow}(T)=\left\{T^{\prime} \mid T \rightarrow^{\star} T^{\prime}\right\}$. Furthermore, if $\mathscr{T}=\left\{T_{1}, T_{2}, \ldots, T_{n}\right\}$ is a finite set of trees, define grow(T)\operatorname{grow}(\mathscr{T}) as the union of all grow(Ti)\operatorname{grow}\left(T_{i}\right) for i=1,2,,ni=1,2,\ldots,n, that is,

$$\operatorname{grow}(\mathscr{T})=\bigcup_{T_{i} \in \mathscr{T}} \operatorname{grow}\left(T_{i}\right)$$

X2\mathbf{X2}: Let us call grow(T)\operatorname{grow}(\mathscr{T}) the set grown from the tree set T\mathscr{T}. That is, it contains all trees that can be obtained by replacing from some TTT \in \mathscr{T}. We may call a set of trees a forest. Informally, the new forest grown from a forest is the forest of all trees grown from all trees in it, in all possible ways. Obviously, the forest grown from any non-empty forest is infinite. But this infinite forest, i.e. grow(T)\operatorname{grow}(\mathscr{T}), does not necessarily contain all trees—more strongly, it does not even necessarily contain “almost all” trees.

X1\mathbf{X1}: Let me add: we call a forest almost complete (or almost contains all trees) if only finitely many trees are not in it. For a finite forest T\mathscr{T}, grow(T)\operatorname{grow}(\mathscr{T}) either contains all trees, or contains almost all trees, or there are infinitely many trees not in it. If this were an OI problem, the problem setter would surely give examples of all three cases in the samples. The key theorem in the book also uses the same definition as ours.

Theorem (Decidability of Almost Completeness): A set of trees is almost complete if only finitely many trees are not in it. Then, for a given finite set of trees T\mathscr{T}, there exists an efficient algorithm to determine whether grow(T)\operatorname{grow}(\mathscr{T}) is almost complete.

X2\mathbf{X2}: This becomes a pure OI problem! Let me restate it in our words: given a finite forest T\mathscr{T}, determine whether grow(T)\operatorname{grow}(\mathscr{T}) is almost complete, i.e. whether only finitely many trees cannot be grown from the trees in the forest.

X1\mathbf{X1}: That is, given a finite set of trees T\mathscr{T}, determine whether there are only finitely many trees TT such that Tgrow(T)T \notin \operatorname{grow}(\mathscr{T}). Saying Tgrow(T)T \notin \operatorname{grow}(\mathscr{T}) means there is no TTT^{\prime} \in \mathscr{T} such that TTT^{\prime} \rightarrow^{\star} T. This is indeed very different from usual OI problems: I cannot even think of an algorithm for it.

X2\mathbf{X2}: Same here, but I have not felt this urge to solve an unknown problem for a long time.

Input Format

This problem contains multiple sets of testdata. The first line of the input file contains a positive integer NN, indicating the number of test cases. Then there are exactly NN test cases. Each test case has the following format:

The first line contains a positive integer mm, indicating the number of trees in the set. Then mm trees are given in the following format:

  • First, a positive integer nn, indicating the number of nodes in the tree. The nodes are numbered 1,2,,n1,2,\ldots,n.
  • Then nn lines follow, each containing two non-negative integers. In the ii-th line, from left to right, there are lil_i and rir_i separated by a space, representing the indices of the left and right child of node ii. If the left (or right) child does not exist, then lil_i (or rir_i) is 00. Of course, a leaf must satisfy li=ri=0l_i=r_i=0.
  • The input is guaranteed to form a tree rooted at node 11. Note: node indices are only for input convenience; any isomorphic trees are considered the same.

Among the mm input trees, some may be isomorphic. After removing duplicates (i.e. keeping only one tree for each isomorphism class), they form a set of trees T\mathscr{T}. You need to determine whether the set grown from this tree set, grow(T)\operatorname{grow}(\mathscr{T}), is almost complete.

Output Format

Output NN lines, giving the answers for the NN test cases. In the ii-th line, output a string: if the set grown from the input tree set of the ii-th test case is almost complete (in other words, only finitely many trees cannot be grown from it), output Almost Complete; otherwise output No. Note the spelling and letter case of the output strings.

1
1
1
0 0
Almost Complete
1
3
3
2 3
0 0
0 0
2
2 0
0 0
2
0 2
0 0
Almost Complete
1
2
3
2 3
0 0
0 0
2
2 0
0 0
No

Hint

Explanation for Sample 2

This sample contains only one test case, where the tree set T\mathscr{T} contains three trees, as shown in the figure below. It is easy to see that only the single-node tree is not in grow(T)\operatorname{grow}(\mathscr{T}). Therefore it contains almost all trees, hence it is almost complete.

img2

Explanation for Sample 3

This sample contains only one test case, where the tree set T\mathscr{T} contains two trees. It is easy to see that for all n2n \geq 2, the chain-shaped tree with nn nodes where every non-leaf node has only a right child is not in grow(T)\operatorname{grow}(\mathscr{T}). Hence there are infinitely many trees not in grow(T)\operatorname{grow}(\mathscr{T}), and T\mathscr{T} is not almost complete.

Sample 4

See surreal/surreal4.in and surreal/surreal4.ans in the contestant directory.


Constraints

All data satisfy: n2×106\sum n \leq 2 \times 10^{6}, m2×106\sum m \leq 2 \times 10^{6}, maxh2×106\max h \leq 2 \times 10^{6}, N102N \leq 10^{2}. Here, n\sum n is the sum of the numbers of nodes of all trees appearing in all test cases of this subtask; m\sum m is the total number of trees appearing in all test cases of this subtask; maxh\max h is the maximum height among all trees appearing in this subtask (a single-node tree has height 11). The entries n\sum n, m\sum m, and maxh\max h in the table below have the same meanings as above, and describe the constraints for each subtask.

Special properties: Below are explanations of the four special properties mentioned in the table.

  • Special property 11: For each test case in this subtask, m4m \leq 4, i.e. the set contains at most 44 trees.
  • Special property 22: For each test case in this subtask, all trees in the set have the same height.
  • Special property 33: For each test case in this subtask, the set contains only chains (in other words, every non-leaf node has exactly one child).
  • Special property 44: For each test case in this subtask, the set contains only trees satisfying one of the following two conditions:
    • Every non-leaf node has exactly one child.
    • There are exactly two leaf nodes with the same parent, and except for these three nodes, every other node has exactly one child.

The specific limits for each subtask are given in the table below:

Subtask ID NN n\sum n m\sum m maxh\max h Special Properties
11 100100 103\le 10^3 1\le 1 None
232\sim 3 2\le 2 Property 11
44 106\le 10^6 4\le 4 None
55 5\le 5 Property 22
66 8\le 8 None
77 9\le 9 Property 22
88 10\le 10 None
99 106\le 10^6 Property 33
1010 2020 103\le 10^3 100\le 100 103\le 10^3 Property 44
1111 2×103\le 2\times 10^3
1212 105\le 10^5
1313 2×105\le 2\times 10^5
1414 800\le 800 200\le 200 800\le 800 None
1515 103\le 10^3 100\le 100 103\le 10^3
1616 2×103\le 2\times 10^3
1717 4040 3×105\le 3\times 10^5
1818 6×105\le 6\times 10^5
1919 9×105\le 9\times 10^5
2020 1.2×106\le 1.2\times 10^6
2121 1.5×106\le 1.5\times 10^6
222522\sim 25 2×106\le 2\times 10^6

Translated by ChatGPT 5