#P10722. Subtree Color Flipping

    ID: 12755 Type: Default 1000ms 256MiB

Subtree Color Flipping

Subtree Color Flipping

You are given a binary tree with \(n\) nodes (numbered from \(1\) to \(n\)) and with node \(1\) as the root. Every node is initially colored either white or black.

Then, you are given \(q\) operations. In each operation, you are given a node \(v\). You need to flip the colors of all nodes in the subtree rooted at \(v\) (i.e. white becomes black and black becomes white).

Your task is to output the final color of each node after all \(q\) operations have been performed.

Note: The tree is provided in a specific format described below.

inputFormat

The input is given in the following format:

n q
S
L1 R1
L2 R2
...
Ln Rn
v1
v2
... 
vq
  • The first line contains two integers \(n\) and \(q\), where \(n\) is the number of nodes and \(q\) is the number of operations.
  • The second line contains a string \(S\) of length \(n\). The \(i\)-th character of \(S\) represents the initial color of node \(i\) ('W' for white, 'B' for black).
  • The next \(n\) lines each contain two integers \(L_i\) and \(R_i\) denoting the left and right child of node \(i\) respectively. If a child does not exist, the value will be 0.
  • The following \(q\) lines each contain a single integer \(v\), indicating an operation on the subtree rooted at node \(v\).

outputFormat

Output a string of length \(n\) where the \(i\)-th character is the final color of node \(i\) after performing all operations ('W' for white, 'B' for black).

sample

3 1
WBW
2 3
0 0
0 0
1
BWB