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<cstdio>
#define M 200002
#define N 100001
inline int min(int x,int y){return x<y?x:y;}
int A[N],C[N],D[N],E[N],G[N],H[N],O[N],P[N],Q,a[N],b[M],c[M],f[N],n,t,x;char d[N];bool B[N],F[N];
void _b(int u)
{
	for(int i=a[u];i;i=b[i])if(c[i]!=f[u])O[++Q]=D[c[i]]-C[c[i]]-1;
	B[u]^(d[u]=='W')?0:C[u]--,P[Q+1]=1,*O=E[u]?A[u]+=E[u],B[u]^=true,C[u]+=G[u]+2,H[u]-G[u]-1:1;
	for(int i=Q;i;i--)P[i]=min(O[i],P[i+1]);
	for(int i=1;i<=Q;i++)O[i]=min(O[i],O[i-1]);
	D[u]=B[u]^(d[u]=='W')?min(C[u]+1,O[Q]+C[u]):min(C[u],O[Q]+C[u]+1),Q=0;
	for(int i=a[u];i;i=b[i])if(c[i]!=f[u])Q++,E[c[i]]=A[u]-A[c[i]],F[c[i]]=B[u]^(bool)A[c[i]],G[c[i]]=C[u]-(A[c[i]]?C[c[i]]+2:0),H[c[i]]=F[c[i]]^(d[u]=='W')?min(G[c[i]],1+min(O[Q-1],P[Q+1])+G[c[i]]):(G[c[i]]++,min(G[c[i]],min(O[Q-1],P[Q+1])+G[c[i]]-1));
	for(int i=a[u];i;i=b[i])if(c[i]!=f[u])Q=0,_b(c[i]);
}
void _a(int u)
{
	int v=1;
	A[u]=d[u]=='W'?1:0;
	for(int i=a[u];i;i=b[i])if(c[i]!=f[u])if(f[c[i]]=u,_a(c[i]),A[c[i]])A[u]+=A[c[i]],B[u]^=true,C[u]+=C[c[i]]+2,v=min(v,D[c[i]]-C[c[i]]-1);
	v+=C[u],D[u]=B[u]^(d[u]=='W')?min(C[u],1+v):min(++C[u],v);
}
int i,u,v;
int main()
{
	for(scanf("%d",&n),i=1;i<n;i++)scanf("%d%d",&u,&v),b[++t]=a[u],c[a[u]=t]=v,b[++t]=a[v],c[a[v]=t]=u;
	for(scanf("%s",d+1),_a(1),_b(i=1),u=0x7fffffff;i<=n;i++)u=min(u,D[i]);
	return 0*printf("%d\n",u);
}