There is a tree with N vertices numbered 1 through N.
The i-th edge connects Vertex xi and yi.
Each vertex is painted white or black.
The initial color of Vertex i is represented by a letter ci.
ci = W
represents the vertex is white; ci = B
represents the vertex is black.
A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly:
The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective?
W
or ci = B
.Subtask 1 (10%): N ≤ 10
Subtask 2 (90%): No additional constraints
Subtask 3 (0%): Sample
Input is given from Standard Input in the following format:
N x1 y1 x2 y2 : xN-1 yN-1 c1c2..cN
Print the minimum number of seconds required to achieve the objective.
5 1 2 2 3 2 4 4 5 WBBWW
5
The objective can be achieved in five seconds, for example, as follows:
6 3 1 4 5 2 6 6 1 3 4 WWBWBB
7
1 B
0
20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW
21
Subtask | Score |
---|---|
1 | 0 |
2 | 10 |
3 | 90 |