시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 (추가 시간 없음) 1024 MB (추가 메모리 없음)123402432.000%

문제

There is a tree with $N$ vertices. Vertices are numbered from $1$ to $N$.

Each vertex is colored either white or black. You can swap the colors of two adjacent vertices any number of times you want. A final tree is any tree formed by completing a finite number of swaps in the original tree. The swap cost of a final tree is equal to the number of swaps you have made.

A white tree is the connected subgraph of the final tree which connects all the white vertices of the final tree using the minimum number of edges. Likewise, a black tree is the connected subgraph of the final tree which connects all the black vertices of the final tree using the minimum number of edges.

The edge cost of a final tree is equal to the sum of the number of edges in the white tree and the number of edges in the black tree. Note that the edge cost is calculated only after all swaps have been completed.

The total cost of a final tree is the sum of its swap cost and edge cost.

Find the minimum possible total cost of a final tree.

입력

The first line of input contains the number of vertices $N$.

The second line of input contains a string $s$ of length $N$ consisting only of W and B. If the $i$-th character of $s$ is W, vertex $i$ is white, and if the $i$-th character is B, vertex $i$ is black. It is guaranteed that there are at least one W and at least one B in the string $s$.

Each of the following $N-1$ lines contains $u$ and $v$ denoting that there is an edge connecting vertex $u$ and vertex $v$.

It is guaranteed that the input forms a valid tree.

출력

The first line of output should contain the minimum possible total cost of a final tree.

제한

  • $2\le N\le 5\times 10^5$
  • $1\le u<v\le N$

예제 입력 1

4
WWBB
1 2
2 3
2 4

예제 출력 1

3

예제 입력 2

8
WBWWWBBB
1 2
2 3
3 4
3 5
1 6
6 7
6 8

예제 출력 2

7

출처

University > KAIST > KAIST ICPC Mock Competition > 2024 KAIST 14th ICPC Mock Competition M번