output_description: Print the minimum number of seconds required to achieve the objective.
  
  * * *
input_description: 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
attempt: import sys
  
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines
  
  N = int(readline())
  data = read().split()
  m = map(int, data[:-1])
  XY = zip(m, m)
  C = [0] + [1 * (x == ord("W")) for x in data[-1]]
  
  graph = [set() for _ in range(N + 1)]
  for x, y in XY:
      graph[x].add(y)
      graph[y].add(x)
  
  deg = [len(x) for x in graph]
  b_leaf = [i for i in range(N + 1) if deg[i] == 1 and not C[i]]
  
  while b_leaf:
      x = b_leaf.pop()
      for y in graph[x]:
          graph[y].remove(x)
          deg[y] -= 1
          if deg[y] == 1 and not C[y]:
              b_leaf.append(y)
      deg[x] = 0
      graph[x].clear()
  
  V = set(i for i, E in enumerate(graph) if E)
  if len(V) <= 2:
      print(len(V))
      exit()
  root = None
  for v in V:
      if deg[v] > 1:
          root = v
          break
  
  parent = [0] * (N + 1)
  order = []
  stack = [root]
  while stack:
      x = stack.pop()
      order.append(x)
      for y in graph[x]:
          if y == parent[x]:
              continue
          parent[y] = x
          stack.append(y)
  
  """
  ・Euler tourの状態から、パスをひとつ除く
  ・葉と葉を結ぶとしてよい。通ると2コスト安くなる点がある
  """
  
  for i in range(N + 1):
      C[i] ^= 1 & deg[i]
  
  cost_full = sum(deg) + sum(C)
  
  # C[i] == 1 となる i を最もたくさん含むパスを探す
  
  opt = 0
  dp = [0] * (N + 1)
  for v in order[::-1]:
      if deg[v] == 1:
          continue
      arr = sorted((dp[w] for w in graph[v] if w != parent[v]), reverse=True) + [0]
      x = arr[0] + arr[1] + C[v]
      if opt < x:
          opt = x
      dp[v] = arr[0] + C[v]
  
  answer = cost_full - 2 * opt
  print(answer)
  
problem_description: 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?
samples: [{"input": "5\n    1 2\n    2 3\n    2 4\n    4 5\n    WBBWW", "output": "5\n    \n\nThe objective can be achieved in five seconds, for example, as follows:\n\n  * Start at Vertex 1. Change the color of Vertex 1 to black.\n  * Move to Vertex 2, then change the color of Vertex 2 to white.\n  * Change the color of Vertex 2 to black.\n  * Move to Vertex 4, then change the color of Vertex 4 to black.\n  * Move to Vertex 5, then change the color of Vertex 5 to black.\n\n* * *"}, {"input": "6\n    3 1\n    4 5\n    2 6\n    6 1\n    3 4\n    WWBWBB", "output": "7\n    \n\n* * *"}, {"input": "1\n    B", "output": "0\n    \n\n* * *"}, {"input": "20\n    2 19\n    5 13\n    6 4\n    15 6\n    12 19\n    13 19\n    3 11\n    8 3\n    3 20\n    16 13\n    7 14\n    3 17\n    7 8\n    10 20\n    11 9\n    8 18\n    8 2\n    10 1\n    6 13\n    WBWBWBBWWWBBWWBBBBBW", "output": "21"}]