Surreal
Time limit1sMemory limit1024 MB
Given a finite set of rooted binary trees, decide whether only finitely many trees cannot be reached from them by repeatedly replacing leaves with trees.
Problem
A tree is defined recursively. A single node is a tree. If a tree is the left or right child of a root node, the result is also a tree. If two trees are the left and right children of a root node, the result is a tree too. Every structure built from these three rules in finitely many steps is called a tree. In other words, the tree here is a non-empty rooted binary tree that distinguishes left children from right children.
Two trees and are isomorphic () if one of the following holds. (1) Both are a single node. (2) Each root has only a left child, and the left subtrees are isomorphic. (3) Each root has only a right child, and the right subtrees are isomorphic. (4) Each root has both children, the left subtrees are isomorphic, and the right subtrees are isomorphic. Equivalently, two trees are isomorphic if they have the same shape when node labels are ignored but left and right children are still distinguished.
Isomorphism is an equivalence relation, and isomorphic trees are treated as the same tree. Two trees are different if they are not isomorphic.
A leaf is a node with no children.
We say may be converted to using a single-step substitution, written , if replacing a leaf of with some tree produces a tree isomorphic to . We say may be converted to by substitution, written , if there exist a natural number and trees such that .
A single-step substitution removes a leaf and attaches a new tree at that position, so a larger subtree grows from the leaf. Substitution allows zero or more such steps, so holds for every tree . A single node can be converted to any tree, and any tree can be converted to infinitely many different trees.
For a tree , define . For a finite set , define . A set of trees is called a forest. The forest grown from a non-empty forest is infinite, but it does not have to contain every tree.
A forest is almost complete if only finitely many trees are not in it. Given a finite set of trees , decide whether only finitely many trees satisfy . Here means that no satisfies .
Input
Each test case contains several instances. The first line holds a positive integer , the number of instances. Each instance starts with an integer , the number of trees, followed by the trees.
A tree is given by an integer , the number of nodes, followed by lines. Line contains two non-negative integers and , the left and right children of node . A missing child is written as 0, so a leaf has . Node 1 is the root. Node labels are only for convenience, because isomorphic trees are considered the same.
The trees in an instance may include isomorphic duplicates. Keeping one tree from each isomorphism class gives the set .
Output
For each instance, print one line. Print Almost Complete if only finitely many trees are outside . Otherwise, print No.
Constraints
For every test case, , , , and . Here is the total number of nodes in all trees of the instances in a test case, is the total number of trees in the instances of a test case, and is the largest tree height in a test case, where a single node has height 1.