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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 1e5;
vector<vector<int>> g(N);
using Value = int;
map<int, int> allBlack[N];
map<int, int> deg[N];
map<int, Value> dp[N][2];
using P = pair<Value, int>;
vector<P> mi0(N, P(0, -1)), mi1(N, P(0, -1));
int calced[N];
int n;
string col;
Value dfs(int i, int back, int p, int f) {
  if(dp[i][back].count(p)) return dp[i][back][p];
  int cnt = 0;
  Value res = 0;
  int now = 'W' == col[i];
  now ^= p != -1;
  if(f || p == -1) {
    // O(deg(i))
    // go only child
    P mi(0, -1);
    allBlack[i][p] = 'B' == col[i];
    for(int j : g[i]) if(j != p) {
      dfs(j, 1, i, f);
      allBlack[i][p] &= allBlack[j][i];
    }
    for(int j : g[i]) if(j != p) if(!allBlack[j][i]) {
      int d = 2 + dfs(j, 1, i, f);
      cnt++;
      now ^= 1;
      res += d;
      P m(- d + (1 + dfs(j, 0, i, f)), j);
      mi = min(mi, m);
      if(!calced[i] && p == -1) {
        if(mi0[i] > m) mi1[i] = mi0[i], mi0[i] = m;
        else if(mi1[i] > m) mi1[i] = m;
      }
    }
    deg[i][p] = cnt;
    if(p == -1) calced[i] = 1;
    if(back == 0 && mi.second != -1) {
      res += mi.first;
      now ^= 1;
    }
  } else {
    // O(1)
    dfs(i, 1, -1, f);
    dfs(p, 1, i, f);
    cnt = deg[i][-1];
    if(!allBlack[p][i]) cnt--;
    allBlack[i][p] = cnt == 0 && 'B' == col[i];
    deg[i][p] = cnt;
    now ^= cnt&1;
    if(back) {
      res = dfs(i, 1, -1, f);
    } else {
      res = dfs(i, 0, -1, f);
      if(!allBlack[p][i]) {
        now ^= 1;
        if(mi0[i].second == p) {
          res -= mi0[i].first;
          res += mi1[i].first;
        }
      }
    }
    if(!allBlack[p][i]) res -= 2 + dfs(p, 1, i, f);
  }
  res += now;
  if(allBlack[i][p]) res = 0;
  return dp[i][back][p] = res;
}
int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n;
  for(int i = 0; i < n - 1; i++) {
    int a, b; cin >> a >> b; a--; b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  cin >> col;
  for(int x = 0; x < 2; x++) dfs(0, x, -1, 1);
  int ans = 1e9;
  for(int i = 0; i < n; i++) for(int x = 0; x < 2; x++) ans = min(ans, dfs(i, x, -1, 0));
  cout << ans << endl;
  return 0;
}