atcoder#ARC161C. [ARC161C] Dyed by Majority (Odd Tree)
[ARC161C] Dyed by Majority (Odd Tree)
Score : points
Problem Statement
You are given a tree with vertices. The vertices are labeled from to , and the -th edge connects vertex and vertex . Moreover, for every vertex, the number of incident edges is odd.
You will color each vertex of the given tree either black ( B
) or white ( W
).
Here, the string obtained by arranging the colors ( B
or W
) of the vertices in the order of vertex labels is called a color sequence.
You are given a color sequence . Determine whether the color sequence can result from performing the following operation once when all vertices are colored, and if it can, find one possible color sequence before the operation.
Operation: For each vertex , let be the color that occupies the majority (more than half) of the colors of the vertices connected to by an edge. For every vertex , change its color to simultaneously.
There are test cases to solve.
Constraints
- The sum of over the test cases in each input file is at most .
- The given edges form a tree.
- Each vertex appears an odd number of times in total as .
- is a string of length consisting of
B
andW
.
Input
The input is given from Standard Input in the following format:
Each test case is in the following format:
Output
Print lines.
For the -th line, if the color sequence can result from performing the operation in the -th test case, print one possible color sequence before the operation; otherwise, print -1
.
If there are multiple possible color sequences before the operation, any of them will be considered correct.
2
4
1 2
1 3
1 4
BWWW
4
1 2
1 3
1 4
BBWW
WBBW
-1
For the first test case, suppose the color sequence before the operation was WBBW
.
In this case,
- for vertex , the colors of the connected vertices are
B
,B
,W
, respectively, withB
occupying the majority; - for vertex , the color of the connected vertex is
W
, withW
occupying the majority; - for vertex , the color of the connected vertex is
W
, withW
occupying the majority; - for vertex , the color of the connected vertex is
W
, withW
occupying the majority.
Therefore, the color sequence after the operation is BWWW
, satisfying the condition.
Similarly, if the color sequence before the operation was WBBB
, WBWB
, or WWBB
, the color sequence after the operation would be BWWW
, and any of these are considered correct.
For the second test case, the color sequence BBWW
cannot result from the operation on the given tree.