This page is still under construction.

Parts of this page are still being built. What you see may change.

Surreal

Time limit1sMemory limit1024 MB

Summary
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.
Level

Hard8 of 10

Topics
Tree, Recursion, Hash map
Solved
No attempts yet

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 TT and T′T' are isomorphic (T≡T′T \equiv T') 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 TT may be converted to T′T' using a single-step substitution, written T→T′T \to T', if replacing a leaf of TT with some tree T′′T'' produces a tree isomorphic to T′T'. We say TT may be converted to T′T' by substitution, written T→∗T′T \to^* T', if there exist a natural number n≥1n \ge 1 and trees T1,T2,…,TnT_1, T_2, \dots, T_n such that T≡T1→T2→⋯→Tn≡T′T \equiv T_1 \to T_2 \to \dots \to T_n \equiv T'.

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 T→∗TT \to^* T holds for every tree TT. A single node can be converted to any tree, and any tree can be converted to infinitely many different trees.

For a tree TT, define grow⁡(T)={T′∣T→∗T′}\operatorname{grow}(T) = \{T' \mid T \to^* T'\}. For a finite set T={T1,T2,…,Tn}\mathscr{T} = \{T_1, T_2, \dots, T_n\}, define grow⁡(T)=⋃i=1ngrow⁡(Ti)\operatorname{grow}(\mathscr{T}) = \bigcup_{i=1}^{n} \operatorname{grow}(T_i). 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 T\mathscr{T}, decide whether only finitely many trees TT satisfy T∉grow⁡(T)T \notin \operatorname{grow}(\mathscr{T}). Here T∉grow⁡(T)T \notin \operatorname{grow}(\mathscr{T}) means that no T′∈TT' \in \mathscr{T} satisfies T′→∗TT' \to^* T.

Input

Each test case contains several instances. The first line holds a positive integer TT, the number of instances. Each instance starts with an integer mm, the number of trees, followed by the mm trees.

A tree is given by an integer nn, the number of nodes, followed by nn lines. Line ii contains two non-negative integers lil_i and rir_i, the left and right children of node ii. A missing child is written as 0, so a leaf has li=ri=0l_i = r_i = 0. Node 1 is the root. Node labels are only for convenience, because isomorphic trees are considered the same.

The mm trees in an instance may include isomorphic duplicates. Keeping one tree from each isomorphism class gives the set T\mathscr{T}.

Output

For each instance, print one line. Print Almost Complete if only finitely many trees are outside grow⁡(T)\operatorname{grow}(\mathscr{T}). Otherwise, print No.

Constraints

For every test case, ∑n≤2×106\sum n \le 2 \times 10^6, ∑m≤2×106\sum m \le 2 \times 10^6, max⁡h≤2×106\max h \le 2 \times 10^6, and T≤102T \le 10^2. Here ∑n\sum n is the total number of nodes in all trees of the instances in a test case, ∑m\sum m is the total number of trees in the instances of a test case, and max⁡h\max h is the largest tree height in a test case, where a single node has height 1.

Examples3

  1. Example 1

    Input
    1
    1
    1
    0 0
    
    Expected output
    Almost Complete
    
  2. Example 2

    Input
    1
    3
    3
    2 3
    0 0
    0 0
    2
    2 0
    0 0
    2
    0 2
    0 0
    
    Expected output
    Almost Complete
    
  3. Example 3

    Input
    1
    2
    3
    2 3
    0 0
    0 0
    2
    2 0
    0 0
    
    Expected output
    No