Group labeled trees that are isomorphic under a bijection preserving edge label differences, and report each group size.
Hard8TreeHash mapDFSSortingNo attempts yetTime limit2sMemory limit512 MBA tree T=(V,E) is a connected graph with no cycle, where V={v1,v2,…,vn} is its vertex set and E={e1,e2,…,en−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→Z is given.
For u,v∈V, define dT(u,v)=mT(u)−mT(v). Two labeled trees T1=(V1,E1) and T2=(V2,E2) are similar when all three conditions hold.

Figure 1: trees T1 and T2 are similar.
The two trees in Figure 1 are similar under this definition, and f is the correspondence drawn in the figure. For instance, dT1(v1,v2)=1−9=−8=9−17=dT2(v4,v5), and dT1(v2,v4)=9−6=3=17−14=dT2(v5,v1), and dT1(v2,v3)=9−7=2=17−15=dT2(v5,v3).
You are given d labeled trees. Similarity is an equivalence relation, so the trees split into groups of mutually similar trees. Find the size of every group.
The first line contains the number of labeled trees d (1≤d≤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) integers for a tree with n vertices. Vertices are numbered 1 to n. The value of n 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) of the vertices v1,v2,…,vn in that order.
2≤n≤70000 and −100000<m(vi)<100000. The labels inside one tree are all distinct.
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 1, and the printed numbers sum to d.