AtCoder Regular Contest 157
E
XXYX Binary Tree
AtCoder
⌀ 2877
原题
← DF →
翻译中…

Problem Statement

翻译中…

You are given a rooted tree with $N$ vertices. The vertices are numbered $1$ to $N$, and the root is vertex $1$. The parent of each vertex $i$ except the root is vertex $P_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 $2$ obtained by concatenating the characters written on the endpoints in the order from the parent $P_i$ to the child $i$. Among the $(N - 1)$ strings obtained in this way,

翻译中…
  • exactly $A$ are XX,
  • exactly $B$ are XY,
  • exactly $C$ are YX, and
  • none is YY.
翻译中…

You have $T$ test cases to solve.

翻译中…

Constraints

翻译中…
  • $T \geq 1$
  • $N \geq 1$
  • For each input, the sum of $N$ over the test cases is at most $10^4$.
  • $A \geq 0$
  • $B \geq 0$
  • $C \geq 0$
  • $A + B + C = N - 1$
  • $1 \leq P_i < i \ \ (2 \leq i \leq N)$
  • Each vertex $k \ (1 \leq k \leq N)$ occurs as a parent $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:

翻译中…

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

翻译中…

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

翻译中…

N A B C
P_2 P_3 \cdots P_N

翻译中…

Output

翻译中…

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

翻译中…

样例

Input #1
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
Output #1
Yes
Yes
No
For the first test case, if you, for instance, write `XXYXYXX` in this order on vertices $1$ to $7$, - from the edge $(1, 2)$, we obtain `XX`, - from the edge $(1, 3)$, we obtain `XY`, - from the edge $(2, 4)$, we obtain `XX`, - from the edge $(2, 5)$, we obtain `XY`, - from the edge $(3, 6)$, we obtain `YX`, and - from the edge $(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.
题目信息
题号
arc157_e
时间限制
3 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。