atcoder#ARC157E. [ARC157E] XXYX Binary Tree

[ARC157E] XXYX Binary Tree

Score : 700700 points

Problem Statement

You are given a rooted tree with NN vertices. The vertices are numbered 11 to NN, and the root is vertex 11. The parent of each vertex ii except the root is vertex PiP_i. Each vertex, including the root, has no child or exactly two children.

Determine whether it is possible to write one of the characters X and Y on each vertex of the given tree to satisfy the following condition.

Condition: For each edge of the tree, consider the string of length 22 obtained by concatenating the characters written on the endpoints in the order from the parent PiP_i to the child ii. Among the (N1)(N - 1) strings obtained in this way,

  • exactly AA are XX,
  • exactly BB are XY,
  • exactly CC are YX, and
  • none is YY.

You have TT test cases to solve.

Constraints

  • T1T \geq 1
  • N1N \geq 1
  • For each input, the sum of NN over the test cases is at most 10410^4.
  • A0A \geq 0
  • B0B \geq 0
  • C0C \geq 0
  • A+B+C=N1A + B + C = N - 1
  • 1Pi<i  (2iN)1 \leq P_i < i \ \ (2 \leq i \leq N)
  • Each vertex k (1kN)k \ (1 \leq k \leq N) occurs as a parent Pi (2iN)P_i \ (2 \leq i \leq N) zero times or twice in total.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

case2\mathrm{case}_2

\vdots

caseT\mathrm{case}_T

Each test case casei (1iT)\mathrm{case}_i \ (1 \leq i \leq T) is in the following format:

NN AA BB CC

P2P_2 P3P_3 \cdots PNP_N

Output

Print TT lines. The ii-th line should contain Yes if there is a way to write characters to satisfy the condition, and No otherwise.

3
7 2 2 2
1 1 2 2 3 3
7 0 2 4
1 1 2 2 3 3
7 2 0 4
1 1 2 2 4 4
Yes
Yes
No

For the first test case, if you, for instance, write XXYXYXX in this order on vertices 11 to 77,

  • from the edge (1,2)(1, 2), we obtain XX,
  • from the edge (1,3)(1, 3), we obtain XY,
  • from the edge (2,4)(2, 4), we obtain XX,
  • from the edge (2,5)(2, 5), we obtain XY,
  • from the edge (3,6)(3, 6), we obtain YX, and
  • from the edge (3,7)(3, 7), we obtain YX.

Each of XX, XY, and YX occurs twice, so the condition is satisfied.

For the second case, one way to satisfy the condition is to write XYYXXXX.

For the third case, there is no way to satisfy the condition.