Under construction

Still being built. Things may change

Counting Similar Trees

Time limit2sMemory limit512 MB

Summary
Group labeled trees that are isomorphic under a bijection preserving edge label differences, and report each group size.
Level

Hard8 of 10

Topics
Tree, Hash map, DFS, Sorting
Solved
No attempts yet

Problem

A tree T=(V,E)T = (V, E) is a connected graph with no cycle, where V={v1,v2,…,vn}V = \{v_1, v_2, \ldots, v_n\} is its vertex set and E={e1,e2,…,en−1}E = \{e_1, e_2, \ldots, e_{n-1}\} is its edge set. A labeled tree is a tree in which every vertex carries a distinct integer, that is, a one-to-one function mT:V→Zm_T : V \to \mathbb{Z} is given.

For u,v∈Vu, v \in V, define dT(u,v)=mT(u)−mT(v)d_T(u, v) = m_T(u) - m_T(v). Two labeled trees T1=(V1,E1)T_1 = (V_1, E_1) and T2=(V2,E2)T_2 = (V_2, E_2) are similar when all three conditions hold.

  1. ∣V1∣=∣V2∣|V_1| = |V_2|.
  2. There is a bijection f:V1→V2f : V_1 \to V_2 such that (u,v)∈E1(u, v) \in E_1 if and only if (f(u),f(v))∈E2(f(u), f(v)) \in E_2. That is, the two trees are isomorphic.
  3. dT1(u,v)=dT2(f(u),f(v))d_{T_1}(u, v) = d_{T_2}(f(u), f(v)) for every (u,v)∈E1(u, v) \in E_1.

Figure 1

Figure 1: trees T1T_1 and T2T_2 are similar.

The two trees in Figure 1 are similar under this definition, and ff is the correspondence drawn in the figure. For instance, dT1(v1,v2)=1−9=−8=9−17=dT2(v4,v5)d_{T_1}(v_1, v_2) = 1 - 9 = -8 = 9 - 17 = d_{T_2}(v_4, v_5), and dT1(v2,v4)=9−6=3=17−14=dT2(v5,v1)d_{T_1}(v_2, v_4) = 9 - 6 = 3 = 17 - 14 = d_{T_2}(v_5, v_1), and dT1(v2,v3)=9−7=2=17−15=dT2(v5,v3)d_{T_1}(v_2, v_3) = 9 - 7 = 2 = 17 - 15 = d_{T_2}(v_5, v_3).

You are given dd labeled trees. Similarity is an equivalence relation, so the trees split into groups of mutually similar trees. Find the size of every group.

Input

The first line contains the number of labeled trees dd (1≤d≤1001 \le d \le 100). Each tree is then given on two lines.

The first line of a tree lists all of its edges. One edge is written as the two numbers of its endpoints, so this line holds 2(n−1)2(n-1) integers for a tree with nn vertices. Vertices are numbered 11 to nn. The value of nn is not given separately, so recover it from the count of integers on this line. The second line of a tree gives the labels m(v1),m(v2),…,m(vn)m(v_1), m(v_2), \ldots, m(v_n) of the vertices v1,v2,…,vnv_1, v_2, \ldots, v_n in that order.

2≤n≤700002 \le n \le 70000 and −100000<m(vi)<100000-100000 < m(v_i) < 100000. The labels inside one tree are all distinct.

Output

Print the size of every group on one line, from smallest to largest, separated by single spaces. The same size can appear more than once. A tree similar to no other tree forms a group of size 11, and the printed numbers sum to dd.

Examples6

  1. Example 1

    Input
    7
    1 2 2 3
    6 18 9
    1 2 2 3 2 4 3 5 3 6
    1 9 7 6 15 30
    3 2 3 6 1 5 4 5 3 5
    14 23 15 9 17 38
    2 1 1 3
    15 3 6
    3 2 3 6 1 5 4 5 3 5 5 7
    14 23 15 9 17 38 11
    2 3 3 1
    7 4 16
    1 2 2 3 2 4 3 5 3 6
    1 9 7 10 15 13
    
    Expected output
    1 1 2 3
    
  2. Example 2

    Input
    1
    1 2
    5 9
    
    Expected output
    1
    
  3. Example 3

    Input
    2
    1 2
    0 4
    2 1
    10 6
    
    Expected output
    2
    
  4. Example 4

    Input
    2
    1 2
    0 4
    1 2
    0 5
    
    Expected output
    1 1
    
  5. Example 5

    Input
    2
    1 2
    -99999 99999
    2 1
    99999 -99999
    
    Expected output
    2
    
  6. Example 6

    Input
    3
    1 2 2 3
    0 1 3
    1 2 2 3
    0 2 3
    1 2 2 3
    1 2 4
    
    Expected output
    1 2