Tree Labeling
Time limit1sMemory limit128 MB
The program counts labelings of a tree with up to 1000 vertices that preserve each label's neighbor label set.
- Level
Hard8 of 10
- Topics
- Tree, Combinatorics, Sorting
- Solved
- No attempts yet
Problem
Consider a tree , where is a set of vertices and is a set of edges. A vertex joined to by an edge is a neighbor of in , and the set of all neighbors of is written .
A labeling of is a one-to-one and onto function .
Two labelings and are equivalent when the following holds: for every vertex there is a vertex such that and . By this definition, a labeling is equivalent to itself.
The figure below shows two equivalent labelings.

Given a tree and a labeling of , write a program that counts the labelings equivalent to . Since is equivalent to itself, include in the count.
Input
Your program reads from standard input. The first line contains the number of test cases ().
The first line of each test case contains the number of vertices () of the tree. Each of the next lines contains two integers and (), which describe an edge joining vertex and vertex . The next line contains integers describing a labeling, where the th number is the label of vertex . All integers are separated by a single space.
Output
Your program writes to standard output. For each test case, print the number of labelings equivalent to on one line. This number can grow very large, so print the exact value without taking a modulo.