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 ll long long
#define dbg1(x) cerr<<#x<<"="<<(x)<<" "
#define dbg2(x) cerr<<#x<<"="<<(x)<<"\n"
#define dbg3(x) cerr<<#x<<"\n"
using namespace std;
#define reg register
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
const int MN=1e5+5,inf=0x7fffffff;
int N;
bool c[MN];
struct edge{int to,nex;}e[MN<<1];
int en,hr[MN];
void ins(int x,int y)
{
	e[++en]=(edge){y,hr[x]};hr[x]=en;
	e[++en]=(edge){x,hr[y]};hr[y]=en;
}
char s[MN];
bool ad[MN];
int f[MN],g[MN],h[MN],ans[MN],sw[MN];
void rw(int &x,int y){if(y<x)x=y;}
std::priority_queue<int>q;
void dfs(int x,int fa=0)
{
	sw[x]=c[x];reg int i,_,tmp=inf;
	#define v e[i].to
	for(i=hr[x];i;i=e[i].nex)if(fa^v)dfs(v,x),sw[x]+=sw[v];
	if(!sw[x])return;
	if(sw[x]==c[x])return(void)(f[x]=g[x]=c[x]^1,ad[x]=ans[x]=h[x]=c[x]);
	for(_=0,i=hr[x];i;i=e[i].nex)if((fa^v)&&sw[v])
		f[x]+=g[v]+2,g[x]+=g[v]+2,rw(tmp,f[v]-g[v]-1),++_;
	_=(_+c[x])&1;ans[x]=min(f[x]+tmp+(_^1),g[x]+_);f[x]+=min(tmp,0)+_;g[x]+=_^1;
	for(tmp=0,i=hr[x];i;i=e[i].nex)if((fa^v)&&sw[v])
	{
		if(sw[v]==sw[x])rw(ans[x],ans[v]);h[x]=2+(ad[v]?-1:1)+h[v]+(c[x]^1);ad[x]=c[x]^1;
		tmp+=g[v]+2;q.push(f[v]-g[v]-1);while(q.size()>2)q.pop();
	}
	if(q.size()==2)
	{
		while(!q.empty())tmp+=min(q.top(),0),q.pop();
		h[x]=tmp+1+(ad[x]=(_^1));
	}
	rw(ans[x],h[x]);
	if(f[x]<h[x]+(ad[x]?-1:1))h[x]=f[x]+(_^1)-_,ad[x]=(_^1);
	if(g[x]<h[x]+(ad[x]?-1:1))h[x]=g[x]+_-(_^1),ad[x]=_;
	rw(ans[x],h[x]);
	while(!q.empty())q.pop();
	#undef v
//	dbg1(x);dbg1(f[x]);dbg1(g[x]);dbg1(h[x]);dbg2(ans[x]);
}
signed main()
{
	N=read();reg int i,x;
	for(i=1;i<N;++i) x=read(),ins(x,read());
	scanf("%s",s+1);
	for(i=1;i<=N;++i) c[i]=s[i]=='W';
	dfs(1);
	return 0*printf("%lld\n",ans[1]);
}