Logo
(追記) (追記ここまで)

32473번 - White-Black-Tree 다국어

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

문제

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번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /