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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author kessido
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FMonochromeCat solver = new FMonochromeCat();
        solver.solve(1, in, out);
        out.close();
    }

    static class FMonochromeCat {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.NextInt();
            Node[] nodes = new Node[n];
            for (int i = 0; i < n; i++) {
                nodes[i] = new Node(i);
            }
            for (int i = 0; i < n - 1; i++) {
                int u = in.NextInt() - 1;
                int v = in.NextInt() - 1;
                nodes[u].edges.add(nodes[v]);
                nodes[v].edges.add(nodes[u]);
            }
            char[] colors = in.next().toCharArray();
            for (int i = 0; i < n; i++) {
                nodes[i].isWhite = colors[i] == 'W' ? 1 : 0;
            }

            long res = Long.MAX_VALUE;
            for (Node node : nodes) res = Math.min(res, node.calculateCostWhenStartingHere(null, false));
            out.println(res);
        }

        class Node implements Comparable<Node> {
            int index;
            ArrayList<Node> edges = new ArrayList<>();
            int isWhite;
            ArrayList<TreeMap<Node, Long>> costWithReturn = new ArrayList<>();
            ArrayList<TreeMap<Node, Long>> costWithoutReturn = new ArrayList<>();

            public Node(int index) {
                this.index = index;
                costWithReturn.add(new TreeMap<>());
                costWithReturn.add(new TreeMap<>());
                costWithoutReturn.add(new TreeMap<>());
                costWithoutReturn.add(new TreeMap<>());
            }


            public int compareTo(Node o) {
                return Integer.compare(index, o.index);
            }

            public long calculateCostWhenStartingHere(Node parent, boolean shouldReturn) {
                TreeMap<Node, Long> toCheck = shouldReturn ? costWithReturn.get(isWhite) : costWithoutReturn.get(isWhite);
                {
                    if (parent != null) {
                        Long res = toCheck.get(parent);
                        if (res != null) return res;
                    }
                }

                int isWhite = this.isWhite;
                long sum = 0;
                for (Node node : edges) {
                    if (node != parent) {
                        if (node.calculateCostWhenStartingHere(this, true) == 0) continue;
                        node.isWhite ^= 1;
                        sum += node.calculateCostWhenStartingHere(this, true) + 2;
                        node.isWhite ^= 1;
                        isWhite ^= 1;
                    }
                }
                long res = sum;
                if (isWhite == 1) res++;
                if (!shouldReturn) {
                    isWhite ^= 1;
                    for (Node node : edges) {
                        if (node != parent) {
                            if (node.calculateCostWhenStartingHere(this, true) == 0) continue;
                            long currentRes = sum;

                            node.isWhite ^= 1;
                            currentRes -= node.calculateCostWhenStartingHere(this, true) + 2;
                            currentRes += node.calculateCostWhenStartingHere(this, false) + 1;
                            node.isWhite ^= 1;

                            if (isWhite == 1) currentRes++;
                            res = Math.min(currentRes, res);
                        }
                    }
                }
                if (parent != null)
                    toCheck.put(parent, res);
                return res;
            }

        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int NextInt() {
            return Integer.parseInt(next());
        }

    }
}