#P10723. Beautiful Tree Transformation

    ID: 12756 Type: Default 1000ms 256MiB

Beautiful Tree Transformation

Beautiful Tree Transformation

You are given a tree with n nodes. Each node is colored either white or black. A tree is considered beautiful if, after removing all white nodes, the remaining nodes (the black ones) still form a tree (i.e. they are connected and acyclic, and non-empty).

You are allowed to perform the following operation: choose a white node and change its color to black. Determine the minimum number of operations required to transform the given tree into a beautiful tree.

Note: If there are no black nodes in the initial tree, you must perform at least one operation.

inputFormat

The input consists of multiple lines:

  • The first line contains an integer n (1 ≤ n ≤ 105) — the number of nodes in the tree.
  • The second line contains a string of length n consisting of the characters 'B' and 'W'. The i-th character represents the color of the i-th node ('B' for black and 'W' for white).
  • The next n-1 lines each contain two integers u and v (1 ≤ u, v ≤ n), representing an undirected edge connecting node u and node v.

outputFormat

Output a single integer — the minimum number of operations required to make the tree beautiful.

sample

5
BWBWB
1 2
1 3
3 4
3 5
0