Logo
(追記) (追記ここまで)

24602번 - Tree Hopping 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 (추가 시간 없음) 1024 MB190806843.312%

문제

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 $t$ (1ドル \le t \le 50{,}000$), which is the number of test cases.

In each test case, the first line of input contains an integer $n$ (2ドル \le n \le 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ドル \le a < b \le n$), representing an edge in the tree between nodes $a$ and $b$.

Each of the next $n$ lines contains an integer $p$ (1ドル \le p \le 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$.

출력

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.

제한

예제 입력 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

1
0

힌트

출처

ICPC > Regionals > North America > Pacific Northwest Regional > 2021 ICPC Pacific Northwest Region > Division 1 M번

ICPC > Regionals > North America > Pacific Northwest Regional > 2021 ICPC Pacific Northwest Region > Division 2 Z번

ICPC > Regionals > North America > South Central USA Regional > 2021 South Central USA Regional Contest > Division 1 J번

ICPC > Regionals > North America > South Central USA Regional > 2021 South Central USA Regional Contest > Division 2 K번

ICPC > Regionals > North America > Mid-Central Regional > Mid-Central USA Programming Contest 2021 H번

ICPC > Regionals > North America > Northeast North America Regional > 2021 Northeast North America Regional Contest B번

ICPC > Regionals > North America > Southeast USA Regional > 2021 Southeast USA Regional Programming Contest > Division 1 J번

ICPC > Regionals > North America > Southeast USA Regional > 2021 Southeast USA Regional Programming Contest > Division 2 K번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /