atcoder#ARC143E. [ARC143E] Reversi
[ARC143E] Reversi
Score : points
Problem Statement
We have a tree with vertices.
The vertices are numbered to ,
and the -th edge connects Vertex and Vertex .
Additionally, each vertex has a reversi piece on it.
The status of the piece on each vertex is given by a string :
if the -th character of is B
, the piece on Vertex is placed with the black side up;
if the -th character of is W
, the piece on Vertex is placed with the white side up.
Determine whether it is possible to perform the operation below times to remove the pieces from all vertices. If it is possible, find the lexicographically smallest possible sequence such that Vertices can be chosen in this order during the process.
- Choose a vertex containing a piece with the white side up, and remove the piece from that vertex. Then, flip all pieces on the vertices adjacent to that vertex.
Notes on reversi pieces
What is the lexicographical order on sequences?
The following is an algorithm to determine the lexicographical order between different sequences and .
Below, let denote the -th element of . Also, if is lexicographically smaller than , we will denote that fact as ; if is lexicographically larger than , we will denote that fact as .
- Let be the smaller of the lengths of and . For each , we check whether and are the same.
- If there is an such that , let be the smallest such . Then, we compare and . If is less than (as a number), we determine that and quit; if is greater than , we determine that and quit.
- If there is no such that , we compare the lengths of and . If is shorter than , we determine that and quit; if is longer than , we determine that and quit.
Constraints
- The given graph is a tree.
- is a string of length consisting of the characters
B
andW
.
Input
Input is given from Standard Input in the following format:
Output
If the objective is unachievable, print -1
. If it is achievable, print the answer in the following format:
4
1 2
2 3
3 4
WBWW
1 2 4 3
4
1 2
2 3
3 4
BBBB
-1
In this case, you cannot perform the operation at all.