atcoder#ARC157E. [ARC157E] XXYX Binary Tree
[ARC157E] XXYX Binary Tree
Score : points
Problem Statement
You are given a rooted tree with vertices. The vertices are numbered to , and the root is vertex . The parent of each vertex except the root is vertex . 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 obtained by concatenating the characters written on the endpoints in the order from the parent to the child . Among the strings obtained in this way,
- exactly are
XX
, - exactly are
XY
, - exactly are
YX
, and - none is
YY
.
You have test cases to solve.
Constraints
- For each input, the sum of over the test cases is at most .
- Each vertex occurs as a parent zero times or twice in total.
Input
The input is given from Standard Input in the following format:
Each test case is in the following format:
Output
Print lines.
The -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 to ,
- from the edge , we obtain
XX
, - from the edge , we obtain
XY
, - from the edge , we obtain
XX
, - from the edge , we obtain
XY
, - from the edge , we obtain
YX
, and - from the edge , 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.