#P6776. [NOI2020] 超现实树
[NOI2020] 超现实树
Description
: 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.
: So the “trees” here are non-empty, rooted binary trees with distinguished left and right children.
: Exactly. Next, the book defines isomorphism of two trees.
Definition: Two trees and are called isomorphic, written , defined by the following four rules:
- Trees consisting of a single node are isomorphic to each other.
- If the roots of two trees both have only a left subtree, and their left subtrees are isomorphic, then these two trees are isomorphic.
- If the roots of two trees both have only a right subtree, and their right subtrees are isomorphic, then these two trees are isomorphic.
- 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.
: 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.
: The book also defines leaves of a tree: same as usual, a leaf is a node with no children.
: That matches the definition we know. Well, mathematicians are really a bit wordy... Probably only someone like would enjoy this style.
: 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 single-step replaces into if replacing some leaf node of with another tree yields a tree isomorphic to , written . We say a tree replaces into , written , if there exist a natural number and trees such that $T \equiv T_{1} \rightarrow T_{2} \rightarrow \cdots \rightarrow T_{n} \equiv T^{\prime}$.
: 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 , we always have . The picture below can help understand the meanings of single-step replacement and replacement.

: 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 , define as the set of trees that 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 as the union of all for , that is,
$$\operatorname{grow}(\mathscr{T})=\bigcup_{T_{i} \in \mathscr{T}} \operatorname{grow}\left(T_{i}\right)$$
: Let us call the set grown from the tree set . That is, it contains all trees that can be obtained by replacing from some . 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. , does not necessarily contain all trees—more strongly, it does not even necessarily contain “almost all” trees.
: 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 , 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 , there exists an efficient algorithm to determine whether is almost complete.
: This becomes a pure OI problem! Let me restate it in our words: given a finite forest , determine whether is almost complete, i.e. whether only finitely many trees cannot be grown from the trees in the forest.
: That is, given a finite set of trees , determine whether there are only finitely many trees such that . Saying means there is no such that . This is indeed very different from usual OI problems: I cannot even think of an algorithm for it.
: 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 , indicating the number of test cases. Then there are exactly test cases. Each test case has the following format:
The first line contains a positive integer , indicating the number of trees in the set. Then trees are given in the following format:
- First, a positive integer , indicating the number of nodes in the tree. The nodes are numbered .
- Then lines follow, each containing two non-negative integers. In the -th line, from left to right, there are and separated by a space, representing the indices of the left and right child of node . If the left (or right) child does not exist, then (or ) is . Of course, a leaf must satisfy .
- The input is guaranteed to form a tree rooted at node . Note: node indices are only for input convenience; any isomorphic trees are considered the same.
Among the 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 . You need to determine whether the set grown from this tree set, , is almost complete.
Output Format
Output lines, giving the answers for the test cases. In the -th line, output a string: if the set grown from the input tree set of the -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 contains three trees, as shown in the figure below. It is easy to see that only the single-node tree is not in . Therefore it contains almost all trees, hence it is almost complete.

Explanation for Sample 3
This sample contains only one test case, where the tree set contains two trees. It is easy to see that for all , the chain-shaped tree with nodes where every non-leaf node has only a right child is not in . Hence there are infinitely many trees not in , and is not almost complete.
Sample 4
See surreal/surreal4.in and surreal/surreal4.ans in the contestant directory.
Constraints
All data satisfy: , , , . Here, is the sum of the numbers of nodes of all trees appearing in all test cases of this subtask; is the total number of trees appearing in all test cases of this subtask; is the maximum height among all trees appearing in this subtask (a single-node tree has height ). The entries , , and 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 : For each test case in this subtask, , i.e. the set contains at most trees.
- Special property : For each test case in this subtask, all trees in the set have the same height.
- Special property : 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 : 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 | Special Properties | ||||
|---|---|---|---|---|---|
| None | |||||
| Property | |||||
| None | |||||
| Property | |||||
| None | |||||
| Property | |||||
| None | |||||
| Property | |||||
| Property | |||||
| None | |||||
Translated by ChatGPT 5
京公网安备 11011102002149号