Problem:
Score : 800 points

Problem Statement

There is a tree with N vertices numbered 1 through N.
The i-th edge connects Vertex x_i and y_i.
Each vertex is painted white or black.
The initial color of Vertex i is represented by a letter c_i.
c_i = W represents the vertex is white; c_i = 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:

Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex.

Invert the color of the vertex where she is currently.

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?

Constraints

1 ≤ N ≤ 10^5

1 ≤ x_i,y_i ≤ N (1 ≤ i ≤ N-1)

The given graph is a tree.

c_i = W or c_i = B.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N-1} y_{N-1}
c_1c_2..c_N

Output

Print the minimum number of seconds required to achieve the objective.

Sample Input 1

5
1 2
2 3
2 4
4 5
WBBWW

Sample Output 1

5

The objective can be achieved in five seconds, for example, as follows:

Start at Vertex 1. Change the color of Vertex 1 to black.

Move to Vertex 2, then change the color of Vertex 2 to white.

Change the color of Vertex 2 to black.

Move to Vertex 4, then change the color of Vertex 4 to black.

Move to Vertex 5, then change the color of Vertex 5 to black.

Sample Input 2

6
3 1
4 5
2 6
6 1
3 4
WWBWBB

Sample Output 2

7

Sample Input 3

1
B

Sample Output 3

0

Sample Input 4

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

Sample Output 4

21

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(clippy::many_single_char_names)]
use proconio::{
    input,
    marker::{Chars, Usize1},
};

fn main() {
    input!(n: usize, xy: [(Usize1, Usize1); n - 1], s: Chars);
    let mut g = vec![vec![]; n];
    for (x, y) in xy {
        g[x].push(y);
        g[y].push(x);
    }

    let mut dp = vec![std::u64::MAX; n];
    let mut ep = vec![std::u64::MAX; n];
    let mut fp = vec![std::u64::MAX; n];
    let mut gp = vec![std::u64::MAX; n];
    #[allow(clippy::too_many_arguments)]
    fn dfs(
        x: usize,
        p: usize,
        s: &[char],
        g: &[Vec<usize>],
        dp: &mut [u64],
        ep: &mut [u64],
        fp: &mut [u64],
        gp: &mut [u64],
    ) -> u64 {
        let mut children = vec![];
        let mut sx = if s[x] == 'W' { 1 } else { 0 };
        for y in g[x].iter().copied().filter(|&y| y != p) {
            let sy = dfs(y, x, s, g, dp, ep, fp, gp);
            if sy != 0 {
                children.push(y);
            }
            sx += sy;
        }
        if sx == 0 {
            return 0;
        }

        let sum_of_dp_y = children.iter().copied().map(|y| dp[y]).sum::<u64>();
        let cnt = children.len() as u64;

        let adjast = |mut x: u64| {
            if (x + 2 - sx) % 2 == 1 {
                x += 1;
            }
            x
        };

        // dp
        dp[x] = adjast(1 + cnt + sum_of_dp_y);

        // ep
        let mut eif: Vec<_> = children.iter().copied().map(|y| dp[y] - ep[y]).collect();
        ep[x] = dp[x].min(adjast(
            cnt + sum_of_dp_y - eif.iter().copied().max().unwrap_or(0),
        ));

        // fp
        let fif: Vec<_> = children.iter().copied().map(|y| dp[y] - fp[y]).collect();
        fp[x] = ep[x].min(adjast(
            cnt + 1 + sum_of_dp_y - fif.iter().copied().max().unwrap_or(0),
        ));
        if 2 <= cnt {
            eif.sort_by_key(|&x| std::cmp::Reverse(x));
            let swp = adjast(cnt - 1 + sum_of_dp_y - eif[0] - eif[1]);
            fp[x] = fp[x].min(swp);
        }

        // gp
        gp[x] = fp[x];
        if s[x] == 'B' && cnt == 1 {
            let y = children[0];
            gp[x] = gp[y];
        }

        sx
    }
    let full = dfs(0, 0, &s, &g, &mut dp, &mut ep, &mut fp, &mut gp);

    println!("{}", if full == 0 { 0 } else { gp[0] });
}