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 Ruby (2.3.3) (judge verdict: Accepted)

N = gets.to_i
es = (0..N).map{ {} }
F = (0..N).map{ {} }
G = (0..N).map{ {} }
(N-1).times {
  x,y = gets.split.map &:to_i
  es[x][y] = es[y][x] = 1
}
Colors = [nil] + gets.chomp.chars.map{ |c| c == "B" ? 0 : 1 }
#p Colors
def dig1(n, es, root)
  visited = {}
  parent = Array.new(n+1, 0)
  queue1 = [root]
  queue2 = [root]
  while queue1.size > 0 do
    u = queue1.pop
    visited[u] = 1
    es[u].keys.each{ |v| 
      next if visited[v]
      queue1 << v 
      queue2 << v 
      parent[v] = u
    }
  end
  # p parent, queue2
  queue2.reverse_each{ |u|
    next if u == root
    f = g = h = 0
    s = es[u].keys.reject{ |v| v == parent[u] }
    t = s.max_by{ |v| F[u][v] - G[u][v] }
    # p u,s
    s.each{ |v|
      h += 1 if F[u][v] > 0
      f += F[u][v]
      g += F[u][v]
    }
    f += ((h > 0 ? h+1 : Colors[u]) + Colors[u]) % 2
    f += (h + Colors[u]) > 0 ? 2 : 0
    if t
      g -= F[u][t] - G[u][t]
    end
    g += ((h > 0 ? h   : Colors[u]) + Colors[u]) % 2
    g += (h + Colors[u]) > 0 ? 1 : 0
    F[parent[u]][u] = f
    G[parent[u]][u] = g
    #p F,G
  }
end

def dig2(n, es, root)
  visited = {}
  parent = Array.new(n+1, 0)
  queue1 = [root]
  while queue1.size > 0 do
    u = queue1.pop
    visited[u] = 1
    es[u].keys.each{ |v| 
      next if visited[v]
      queue1 << v 
      parent[v] = u
    }
    s = es[u].keys
    t = s.max_by{ |v| F[u][v] - G[u][v] }
    w = s.reject{ |v| v == t }.max_by{ |v| F[u][v] - G[u][v] }
    f0 = g0 = h0 = 0
    # p u,s
    #p u, s
    s.each{ |v|
      h0 += 1 if F[u][v] > 0
      f0 += F[u][v]
      g0 += F[u][v]
    }
    s.each{ |v|
      h = h0 - (F[u][v] > 0 ? 1 : 0)
      f = f0 - F[u][v]
      f += ((h > 0 ? h+1 : Colors[u]) + Colors[u]) % 2
      f += (h + Colors[u]) > 0 ? 2 : 0
      g = g0 - F[u][v]
      if s.size > 1
        x = t == v ? w : t
        g -= F[u][x] - G[u][x]
      end
      g += ((h > 0 ? h   : Colors[u]) + Colors[u]) % 2
      g += (h + Colors[u]) > 0 ? 1 : 0
      F[v][u] = f
      G[v][u] = g
      #p F,G
    }
    h = h0
    f = f0
    f += ((h > 0 ? h+1 : Colors[u]) + Colors[u]) % 2
    f += (h + Colors[u]) > 0 ? 2 : 0
    g = g0
    if t
      g -= F[u][t] - G[u][t]
    end
    g += ((h > 0 ? h   : Colors[u]) + Colors[u]) % 2
    g += (h + Colors[u]) > 0 ? 1 : 0
    F[0][u] = f
    G[0][u] = g
  end
end

dig1(N, es, 1)
dig2(N, es, 1)

#p F, G
p G[0].values.min