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>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int n;
vi a,used;
vvi g;
vi dp,DP;

int f(int x){
	return (x?1:-1);
}

int dfs(int v,int p,int d,int t){
	dp[v]=t-d;
	t+=f(a[v]);
	for(auto u:g[v]) if(u!=p) dp[v]=min(dp[v],dfs(u,v,d+1,t));
	return dp[v];
}

void DFS(int v,int p,int t,int sum){
	vp b;
	DP[v]=min(dp[v],t);
	for(auto u:g[v]) if(u!=p) b.push_back({dp[u],u});
	sort(b.begin(),b.end());
	for(auto u:g[v]) if(u!=p){
		int T=min(t,sum);
		if(b[0].second!=u) T=min(T,b[0].first);
		else if(b.size()>1) T=min(T,b[1].first);
		T+=f(a[u])-1;
		DFS(u,v,T,sum);
	}
}

int main(){
	cin>>n;
	g=vvi(n);
	a=used=vi(n);
	for(int i=0;i<1;i++){
	vi d(n);
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].push_back(v);
		g[v].push_back(u);
		d[u]++;d[v]++;
	}
	for(int i=0;i<n;i++){
		char c;
		cin>>c;
		if(c=='B') a[i]++;
	}
	if(a==vi(n,1)){
		cout<<0<<endl;
		return 0;
	}
	queue<int> q;
	for(int i=0;i<n;i++) if(d[i]==1&&a[i]) q.push(i),used[i]=1;
	while(!q.empty()){
		int v=q.front();
		q.pop();
		for(auto u:g[v]) if(!used[u]){
			d[u]--;
			if(d[u]==1&&a[u]) q.push(u),used[u]=1;
		}
	}
	map<int,int> m;
	int t=0;
	for(int i=0;i<n;i++) if(!used[i]) m[i]=t++;
	vvi G(t);
	vi a_(t);
	for(int i=0;i<n;i++) if(!used[i]){
		for(auto j:g[i]) if(!used[j]) G[m[i]].push_back(m[j]);
		a_[m[i]]=a[i];
	}
	n=t;a=a_;g=G;}
	for(int i=0;i<n;i++) (a[i]+=g[i].size())%=2;
	dp=DP=vi(n);
	int t=0;
	for(auto i:a) if(!i) t++;
	int res=inf;
	dfs(0,-1,0,t);
	DFS(0,-1,inf,t);
	for(int i=0;i<n;i++) res=min(res,DP[i]);
	cout<<2*n+res-2<<endl;
}