AtCoder Regular Contest 143
E
Reversi
AtCoder
⌀ 2521
原题
← DF →
翻译中…

Problem Statement

翻译中…

We have a tree with $N$ vertices. The vertices are numbered $1$ to $N$, and the $i$-th edge connects Vertex $A_i$ and Vertex $B_i$. Additionally, each vertex has a reversi piece on it. The status of the piece on each vertex is given by a string $S$: if the $i$-th character of $S$ is B, the piece on Vertex $i$ is placed with the black side up; if the $i$-th character of $S$ is W, the piece on Vertex $i$ is placed with the white side up.

翻译中…

Determine whether it is possible to perform the operation below $N$ times to remove the pieces from all vertices. If it is possible, find the lexicographically smallest possible sequence $P_1, P_2, \ldots, P_N$ such that Vertices $P_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 A reversi piece has a black side and a white side, and flipping it changes which side faces up.

翻译中…

What is the lexicographical order on sequences? The following is an algorithm to determine the lexicographical order between different sequences $S$ and $T$. Below, let $S_i$ denote the $i$-th element of $S$. Also, if $S$ is lexicographically smaller than $T$, we will denote that fact as $S \lt T$; if $S$ is lexicographically larger than $T$, we will denote that fact as $S \gt T$. Let $L$ be the smaller of the lengths of $S$ and $T$. For each $i=1,2,\dots,L$, we check whether $S_i$ and $T_i$ are the same. If there is an $i$ such that $S_i \neq T_i$, let $j$ be the smallest such $i$. Then, we compare $S_j$ and $T_j$. If $S_j$ is less than $T_j$ (as a number), we determine that $S \lt T$ and quit; if $S_j$ is greater than $T_j$, we determine that $S \gt T$ and quit. If there is no $i$ such that $S_i \neq T_i$, we compare the lengths of $S$ and $T$. If $S$ is shorter than $T$, we determine that $S \lt T$ and quit; if $S$ is longer than $T$, we determine that $S \gt T$ and quit.

翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 2\times 10^5$
  • $1 \leq A_i, B_i \leq N$
  • The given graph is a tree.
  • $S$ is a string of length $N$ consisting of the characters B and W.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
S

翻译中…

Output

翻译中…

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

翻译中…

P_1 P_2 \cdots P_N

翻译中…

样例

Input #1
4
1 2
2 3
3 4
WBWW
Output #1
1 2 4 3
Input #2
4
1 2
2 3
3 4
BBBB
Output #2
-1
In this case, you cannot perform the operation at all.
题目信息
题号
arc143_e
时间限制
2 s
内存限制
1024 MB

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