Hungry Arachnid

시간 제한1초메모리 제한1024 MB

요약
그림자에 속한 정점 수를 일정하게 유지하면서 거미가 다리 하나를 파리의 정점으로 옮길 수 있는지 판정한다.
난이도

어려움10점 중 9점

유형
트리, DFS, 구현, 그리디
정답자
아직 제출이 없습니다

문제

You are given a tree on nn nodes rooted at node 11. A spider and a fly are in the tree. The spider has three legs which are initially on nodes aa, bb, and cc. The fly is on node ff and does not move.

Some nodes are considered to be in the shadow of the spider. A node is in the shadow of the spider if it lies on any of the three shortest paths between its legs, aa--bb, aa--cc, and bb--cc.

The spider can move its legs from vertices aa, bb, cc to vertices a′a', b′b', c′c' if the size of its shadow remains constant and max⁡dist(a,a′),dist(b,b′),dist(c,c′)≤1\max\\{\textrm{dist}(a, a'), \textrm{dist}(b, b'), \textrm{dist}(c, c')\\}\leq 1. The function dist(u,,v)\textrm{dist}(u,\\,v) indicates the number of edges on the shortest path between nodes uu and vv in the tree.

For example, here is one possible sequence of two moves by a spider with 66 nodes in its shadow. The vertices that have a red outline are in the shadow of the spider, and the vertices that are colored red are the spider's legs.

The spider eats through its legs. Determine whether the spider can move any of its legs to the fly's location, after any number of moves (possibly zero).

입력

The first line of the input contains a single integer tt (1≤t≤1041\le t\le 10^4) --- the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\leq n\leq 2\cdot 10^5) --- the number of vertices in the tree.

The next line of each test case contains n−1n-1 integers p_2,,p_3,,…,,p_np\_2,\\,p\_3,\\,\ldots,\\,p\_n (1≤p_i<i1 \le p\_i < i) --- the parents of each vertex in the tree, except the root.

The next line of each test case contains three integers aa, bb, and cc (1≤a,,b,,c≤n1\leq a,\\,b,\\,c\leq n) --- the initial positions of each of the spider's legs.

The fourth and final line of each test case contains an integer ff (1≤f≤n1\leq f\leq n) --- the position of the fly.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

출력

For each test case, print "YES" if the spider is able to catch the fly, and "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

힌트

In the first test case, all legs of the spider are initially on vertex 22, so that is the only vertex in the shadow. By moving all legs to vertex 11 at the same time, the spider can reach the food while keeping its shadow the same size.

In the second test case, the spider can use this move to reach the food with one of its legs:

In the third test case, the food is located at vertex 11, which is in the shadow of the spider, but the spider cannot move any of its legs to the food:

예제1

  1. 예제 1

    입력
    7
    2
    1
    2 2 2
    1
    6
    1 1 3 1 5
    2 4 5
    1
    6
    1 1 3 1 5
    2 4 6
    1
    18
    1 2 3 2 5 3 1 7 7 7 4 2 12 4 12 4 15
    12 15 12
    16
    12
    1 1 3 4 5 5 7 4 9 10 9
    1 6 11
    12
    12
    1 1 3 4 5 5 7 4 9 10 9
    1 6 11
    4
    12
    1 1 3 4 5 5 7 4 9 10 9
    1 6 11
    6
    
    예상 출력
    Yes
    Yes
    No
    Yes
    Yes
    No
    Yes