atcoder#ARC143E. [ARC143E] Reversi

[ARC143E] Reversi

Score : 700700 points

Problem Statement

We have a tree with NN vertices. The vertices are numbered 11 to NN, and the ii-th edge connects Vertex AiA_i and Vertex BiB_i. Additionally, each vertex has a reversi piece on it. The status of the piece on each vertex is given by a string SS: if the ii-th character of SS is B, the piece on Vertex ii is placed with the black side up; if the ii-th character of SS is W, the piece on Vertex ii is placed with the white side up.

Determine whether it is possible to perform the operation below NN times to remove the pieces from all vertices. If it is possible, find the lexicographically smallest possible sequence P1,P2,,PNP_1, P_2, \ldots, P_N such that Vertices P1,P2,,PNP_1, P_2, \ldots, P_N 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 SS and TT.

Below, let SiS_i denote the ii-th element of SS. Also, if SS is lexicographically smaller than TT, we will denote that fact as S<TS \lt T; if SS is lexicographically larger than TT, we will denote that fact as S>TS \gt T.

  1. Let LL be the smaller of the lengths of SS and TT. For each i=1,2,,Li=1,2,\dots,L, we check whether SiS_i and TiT_i are the same.
  2. If there is an ii such that SiTiS_i \neq T_i, let jj be the smallest such ii. Then, we compare SjS_j and TjT_j. If SjS_j is less than TjT_j (as a number), we determine that S<TS \lt T and quit; if SjS_j is greater than TjT_j, we determine that S>TS \gt T and quit.
  3. If there is no ii such that SiTiS_i \neq T_i, we compare the lengths of SS and TT. If SS is shorter than TT, we determine that S<TS \lt T and quit; if SS is longer than TT, we determine that S>TS \gt T and quit.

Constraints

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1Ai,BiN1 \leq A_i, B_i \leq N
  • The given graph is a tree.
  • SS is a string of length NN consisting of the characters B and W.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

\vdots

AN1A_{N-1} BN1B_{N-1}

SS

Output

If the objective is unachievable, print -1. If it is achievable, print the answer in the following format:

P1P_1 P2P_2 \cdots PNP_N

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.