Counting Similar Trees

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

Hard8TreeHash mapDFSSortingNo attempts yetTime limit2sMemory limit512 MB

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,,en1}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:VZm_T : V \to \mathbb{Z} is given.

For u,vVu, 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:V1V2f : 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)=19=8=917=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)=96=3=1714=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)=97=2=1715=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 (1d1001 \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(n1)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.

2n700002 \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.