2021 South Central USA Regional Contest — Division II

Start

2022-03-05 13:00 CST

2021 South Central USA Regional Contest — Division II

End

2022-03-05 18:00 CST
Contest is over.

Problem J
Tree Hopping

You are given a tree and a permutation of its vertices. It can be proven that for any tree and any pair of source/destination nodes, there is some permutation of the nodes where the first node is the source, the last node is the destination, and the distance between adjacent nodes in the permutation is less than or equal to three.

Your job will be to write a verifier for this property. Given such a permutation and the tree, validate whether the distance between adjacent nodes in the permutation is less than or equal to three.

Input

The first line of input contains an integer t (1 ≤ t ≤ 50,000), which is the number of test cases.

In each test case, the first line of input contains an integer n (2 ≤ n ≤ 100,000), which is the number of nodes in the tree. The nodes are numbered from 1 to n.

Each of the next n-1 lines contains a pair of integers a and b (1 ≤ a < b ≤ n), representing an edge in the tree between nodes a and b.

Each of the next n lines contains an integer p (1 ≤ p ≤ n, all values distinct). This is the permutation of the nodes.

The sum of the values of n over all test cases will not exceed 100,000.

Output

For each test case, output a single line with a single integer, which is 1 if the given permutation satisfies the constraint that every pair of adjacent nodes in the permutation has distance less than or equal to three in the tree. Output 0 if the given permutation does not satisfy this constraint.

Sample Input 1 Sample Output 1
2
5
1 2
2 3
3 4
4 5
1
3
2
5
4
5
1 2
2 3
3 4
4 5
1
5
2
3
4
1
0