Counting Similar Trees
Time limit2sMemory limit512 MB
Group labeled trees that are isomorphic under a bijection preserving edge label differences, and report each group size.
Problem
A tree is a connected graph with no cycle, where is its vertex set and 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 is given.
For , define . Two labeled trees and are similar when all three conditions hold.
- .
- There is a bijection such that if and only if . That is, the two trees are isomorphic.
- for every .

Figure 1: trees and are similar.
The two trees in Figure 1 are similar under this definition, and is the correspondence drawn in the figure. For instance, , and , and .
You are given 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 (). 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 integers for a tree with vertices. Vertices are numbered to . The value of is not given separately, so recover it from the count of integers on this line. The second line of a tree gives the labels of the vertices in that order.
and . 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 , and the printed numbers sum to .