Tree Hopping

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

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.

입력

The first line of input contains an integer tt (1t50,0001 \le t \le 50{,}000), which is the number of test cases.

In each test case, the first line of input contains an integer nn (2n100,0002 \le n \le 100{,}000), which is the number of nodes in the tree. The nodes are numbered from 11 to nn.

Each of the next n1n-1 lines contains a pair of integers aa and bb (1a<bn1 \le a < b \le n), representing an edge in the tree between nodes aa and bb.

Each of the next nn lines contains an integer pp (1pn1 \le p \le n, all values distinct). This is the permutation of the nodes.

The sum of the values of nn over all test cases will not exceed 100,000100{,}000.

출력

For each test case, output a single line with a single integer, which is 11 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 00 if the given permutation does not satisfy this constraint.