This page is still under construction.

Parts of this page are still being built. What you see may change.

Tree Labeling

Time limit1sMemory limit128 MB

Summary
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 T=(V,E)T = (V, E), where VV is a set of vertices and EE is a set of edges. A vertex joined to vv by an edge is a neighbor of vv in TT, and the set of all neighbors of vv is written N(v)N(v).

A labeling of TT is a one-to-one and onto function f:V→{1,2,…,∣V∣}f : V \to \{1, 2, \dots, |V|\}.

Two labelings ff and gg are equivalent when the following holds: for every vertex u∈Vu \in V there is a vertex v∈Vv \in V such that f(u)=g(v)f(u) = g(v) and {f(u′)∣u′∈N(u)}={g(v′)∣v′∈N(v)}\{f(u') \mid u' \in N(u)\} = \{g(v') \mid v' \in N(v)\}. By this definition, a labeling ff is equivalent to itself.

The figure below shows two equivalent labelings.

Two equivalent labelings

Given a tree TT and a labeling ff of TT, write a program that counts the labelings equivalent to ff. Since ff is equivalent to itself, include ff in the count.

Input

Your program reads from standard input. The first line contains the number of test cases TT (1≤T≤201 \le T \le 20).

The first line of each test case contains the number of vertices NN (1≤N≤10001 \le N \le 1000) of the tree. Each of the next N−1N-1 lines contains two integers ii and jj (1≤i,j≤N1 \le i, j \le N), which describe an edge joining vertex ii and vertex jj. The next line contains NN integers describing a labeling, where the iith number is the label of vertex ii. 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 ff on one line. This number can grow very large, so print the exact value without taking a modulo.

Examples1

  1. Example 1

    Input
    2
    9
    2 8
    1 2
    2 3
    3 4
    4 5
    5 6
    4 7
    2 9
    1 9 2 6 3 7 4 5 8
    7
    1 5
    2 5
    5 7
    6 7
    3 6
    4 6
    7 1 6 2 5 3 4
    
    Expected output
    6
    8