Language: C++

// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>

// type alias
using lint = long long;
using ldouble = long double;
template <class T>
using greater_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

/* ----- class ----- */

template <class Cost = int>
struct Edge {
    int from, to;
    Cost cost;
    Edge(int from = -1, int to = -1, Cost cost = 1)
        : from(from), to(to), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
using Edges = std::vector<Edge<Cost>>;

template <class Cost = int>
class Graph {
public:
    int size;
    std::vector<std::vector<Edge<Cost>>> path;

    explicit Graph(int N = 0) : size(N), path(size) {}
    void span(int from, int to, Cost cost = 1) {
        path[from].push_back(Edge<Cost>(from, to, cost));
    }
    std::vector<Edge<Cost>>& operator[](int v) { return path[v]; }
};

/* ----- Output Functions for Debugging ----- */

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v);
template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T> v);
template <class L, class R>
std::ostream& operator<<(std::ostream& os, std::pair<L, R> p);
template <class K, class T>
std::ostream& operator<<(std::ostream& os, std::map<K, T> v);
template <class T>
std::ostream& operator<<(std::ostream& os, std::stack<T> s);
template <class T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q);
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q);
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q);
template <class T>
std::ostream& operator<<(std::ostream& os, Edge<T> e);

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
    os << "[";
    for (auto vv : v) os << vv << ",";
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}
template <class L, class R>
std::ostream& operator<<(std::ostream& os, std::pair<L, R> p) {
    return os << "(" << p.first << "," << p.second << ")";
}
template <class K, class T>
std::ostream& operator<<(std::ostream& os, std::map<K, T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::stack<T> s) {
    os << "[";
    while (!s.empty()) {
        os << s.top() << ",";
        s.pop();
    }
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q) {
    os << "[";
    while (!q.empty()) {
        os << q.front() << ",";
        q.pop();
    }
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, Edge<T> e) {
    return os << "(" << e.from << "->" << e.to << ":" << e.cost << ")";
}

/* ----- Short Functions ----- */

template <class T>
inline T sq(T a) { return a * a; }

template <class T>
inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <class T>
T gcd(T a, T b) {
    while (b > 0) {
        a %= b;
        std::swap(a, b);
    }
    return a;
}

template <class T, class U>
T ipow(T b, U n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= b;
        n >>= 1;
        b *= b;
    }
    return ret;
}

// 0-indexed
template <class T, class U>
inline T kthbit(T a, U k) { return (a >> k) & 1; }

template <class T, class U>
inline T mask(T a, U k) { return a & ((1 << k) - 1); }

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < v.size(); ++i) rev[v[i]] = i;
    return rev;
}

template <class T>
T Vec(T v) { return v; }

template <class T, class... Ts>
auto Vec(size_t l, Ts... ts) {
    return std::vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

/* ----- Constants ----- */

// const int INF = std::numeric_limits<int>::max() / 3;
// const lint INF = std::numeric_limits<lint>::max() / 3;
// const ldouble PI = acos(-1);
// const ldouble EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

using namespace std;

int main() {
    int N;
    cin >> N;

    Graph<> graph(N);
    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        graph.span(u, v);
        graph.span(v, u);
    }

    vector<bool> color(N);
    // false = black
    for (auto&& e : color) {
        char c;
        cin >> c;
        e = (c == 'W');
    }

    int r;
    for (r = 0; r < N && !color[r]; ++r) {}

    if (r == N) {
        cout << 0 << endl;
        return 0;
    }

    int ans = 0, diam = -1;

    // vを端点とする、白を最も多く含むパスの白の数を返す
    // 部分木に白がなければ-1
    function<int(int, int)> dfs = [&](int v, int p) {
        vector<int> ch;
        for (auto e : graph[v]) {
            if (e.to == p) continue;
            int res = dfs(e.to, v);
            if (res >= 0) ch.push_back(res);
        }

        // 訪れる必要のある子がいなくて自分は既に黒=>探索不要
        if (ch.empty() && !color[v]) return -1;

        color[v] = (color[v] + ch.size() + (p >= 0)) % 2;
        // 根だけは例外で、入るときの塗り替えがない
        ans += ch.size() * 2;

        ch.push_back(0), ch.push_back(0);
        sort(ch.rbegin(), ch.rend());
        diam = max(diam, color[v] + ch[0] + ch[1]);

        return color[v] + ch[0];
    };
    dfs(r, -1);

    cout << ans - diam << endl;
    return 0;
}

#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>

// type alias
using lint = long long;
using ldouble = long double;
template <class T>
using greater_priority_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

/* ----- class ----- */

template <class Cost = int>
struct Edge {
    int from, to;
    Cost cost;
    Edge(int from = -1, int to = -1, Cost cost = 1)
        : from(from), to(to), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
using Edges = std::vector<Edge<Cost>>;

template <class Cost = int>
class Graph {
public:
    int size;
    std::vector<std::vector<Edge<Cost>>> path;

    explicit Graph(int N = 0) : size(N), path(size) {}
    void span(int from, int to, Cost cost = 1) {
        path[from].push_back(Edge<Cost>(from, to, cost));
    }
    std::vector<Edge<Cost>>& operator[](int v) { return path[v]; }
};

/* ----- Output Functions for Debugging ----- */

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v);
template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T> v);
template <class L, class R>
std::ostream& operator<<(std::ostream& os, std::pair<L, R> p);
template <class K, class T>
std::ostream& operator<<(std::ostream& os, std::map<K, T> v);
template <class T>
std::ostream& operator<<(std::ostream& os, std::stack<T> s);
template <class T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q);
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q);
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q);
template <class T>
std::ostream& operator<<(std::ostream& os, Edge<T> e);

template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
    os << "[";
    for (auto vv : v) os << vv << ",";
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::set<T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}
template <class L, class R>
std::ostream& operator<<(std::ostream& os, std::pair<L, R> p) {
    return os << "(" << p.first << "," << p.second << ")";
}
template <class K, class T>
std::ostream& operator<<(std::ostream& os, std::map<K, T> v) {
    os << "{";
    for (auto vv : v) os << vv << ",";
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::stack<T> s) {
    os << "[";
    while (!s.empty()) {
        os << s.top() << ",";
        s.pop();
    }
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::queue<T> q) {
    os << "[";
    while (!q.empty()) {
        os << q.front() << ",";
        q.pop();
    }
    return os << "]";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
    os << "{";
    while (!q.empty()) {
        os << q.top() << ",";
        q.pop();
    }
    return os << "}";
}
template <class T>
std::ostream& operator<<(std::ostream& os, Edge<T> e) {
    return os << "(" << e.from << "->" << e.to << ":" << e.cost << ")";
}

/* ----- Short Functions ----- */

template <class T>
inline T sq(T a) { return a * a; }

template <class T>
inline T iceil(T n, T d) { return (n + d - 1) / d; }

template <class T>
T gcd(T a, T b) {
    while (b > 0) {
        a %= b;
        std::swap(a, b);
    }
    return a;
}

template <class T, class U>
T ipow(T b, U n) {
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= b;
        n >>= 1;
        b *= b;
    }
    return ret;
}

// 0-indexed
template <class T, class U>
inline T kthbit(T a, U k) { return (a >> k) & 1; }

template <class T, class U>
inline T mask(T a, U k) { return a & ((1 << k) - 1); }

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < v.size(); ++i) rev[v[i]] = i;
    return rev;
}

template <class T>
T Vec(T v) { return v; }

template <class T, class... Ts>
auto Vec(size_t l, Ts... ts) {
    return std::vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

/* ----- Constants ----- */

// const int INF = std::numeric_limits<int>::max() / 3;
// const lint INF = std::numeric_limits<lint>::max() / 3;
// const ldouble PI = acos(-1);
// const ldouble EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

using namespace std;

int main() {
    int N;
    cin >> N;

    Graph<> graph(N);
    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        graph.span(u, v);
        graph.span(v, u);
    }

    vector<bool> color(N);
    // false = black
    for (auto&& e : color) {
        char c;
        cin >> c;
        e = (c == 'W');
    }

    int r;
    for (r = 0; r < N && !color[r]; ++r) {}

    if (r == N) {
        cout << 0 << endl;
        return 0;
    }

    int ans = 0, diam = 0;

    // vを端点とする、白を最も多く含むパスの白の数を返す
    // 部分木に白がなければ-1
    function<int(int, int)> dfs = [&](int v, int p) {
        vector<int> ch;
        for (auto e : graph[v]) {
            if (e.to == p) continue;
            int res = dfs(e.to, v);
            if (res >= 0) ch.push_back(res);
        }

        // 訪れる必要のある子がいなくて自分は既に黒=>探索不要
        if (ch.empty() && !color[v]) return -1;

        color[v] = (color[v] + ch.size() + (p >= 0)) % 2;
        // 根だけは例外で、入るときの塗り替えがない
        ans += ch.size() * 2;

        ch.push_back(0), ch.push_back(0);
        sort(ch.rbegin(), ch.rend());
        diam = max(diam, color[v] + ch[0] + ch[1]);

        return color[v] + ch[0];
    };
    dfs(r, -1);

    cout << ans - diam << endl;
    return 0;
}


Compiler output:
a.cc:9:21: error: missing terminating > character
    9 | #include <cmath// IO
      |                     ^
a.cc:308:11: error: redefinition of default argument for 'class Cost'
  308 | template <class Cost = int>
      |           ^~~~~
a.cc:46:11: note: original definition appeared here
   46 | template <class Cost = int>
      |           ^~~~~
a.cc:322:11: error: redefinition of default argument for 'class Cost'
  322 | template <class Cost = int>
      |           ^~~~~
a.cc:60:11: note: original definition appeared here
   60 | template <class Cost = int>
      |           ^~~~~
a.cc:357:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>)'
  357 | std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
      |               ^~~~~~~~
a.cc:95:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>)' previously declared here
   95 | std::ostream& operator<<(std::ostream& os, std::vector<T> v) {
      |               ^~~~~~~~
a.cc:363:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::set<T>)'
  363 | std::ostream& operator<<(std::ostream& os, std::set<T> v) {
      |               ^~~~~~~~
a.cc:101:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::set<T>)' previously declared here
  101 | std::ostream& operator<<(std::ostream& os, std::set<T> v) {
      |               ^~~~~~~~
a.cc:369:15: error: redefinition of 'template<class L, class R> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)'
  369 | std::ostream& operator<<(std::ostream& os, std::pair<L, R> p) {
      |               ^~~~~~~~
a.cc:107:15: note: 'template<class L, class R> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)' previously declared here
  107 | std::ostream& operator<<(std::ostream& os, std::pair<L, R> p) {
      |               ^~~~~~~~
a.cc:373:15: error: redefinition of 'template<class K, class T> std::ostream& operator<<(std::ostream&, std::map<K, T>)'
  373 | std::ostream& operator<<(std::ostream& os, std::map<K, T> v) {
      |               ^~~~~~~~
a.cc:111:15: note: 'template<class K, class T> std::ostream& operator<<(std::ostream&, std::map<K, T>)' previously declared here
  111 | std::ostream& operator<<(std::ostream& os, std::map<K, T> v) {
      |               ^~~~~~~~
a.cc:379:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::stack<T>)'
  379 | std::ostream& operator<<(std::ostream& os, std::stack<T> s) {
      |               ^~~~~~~~
a.cc:117:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::stack<T>)' previously declared here
  117 | std::ostream& operator<<(std::ostream& os, std::stack<T> s) {
      |               ^~~~~~~~
a.cc:388:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::queue<T>)'
  388 | std::ostream& operator<<(std::ostream& os, std::queue<T> q) {
      |               ^~~~~~~~
a.cc:126:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::queue<T>)' previously declared here
  126 | std::ostream& operator<<(std::ostream& os, std::queue<T> q) {
      |               ^~~~~~~~
a.cc:397:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::priority_queue<T>)'
  397 | std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q) {
      |               ^~~~~~~~
a.cc:135:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::priority_queue<T>)' previously declared here
  135 | std::ostream& operator<<(std::ostream& os, std::priority_queue<T> q) {
      |               ^~~~~~~~
a.cc:406:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::priority_queue<T, std::vector<_Tp>, std::greater<T> >)'
  406 | std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
      |               ^~~~~~~~
a.cc:144:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::priority_queue<T, std::vector<_Tp>, std::greater<T> >)' previously declared here
  144 | std::ostream& operator<<(std::ostream& os, std::priority_queue<T, std::vector<T>, std::greater<T>> q) {
      |               ^~~~~~~~
a.cc:415:15: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, Edge<Cost>)'
  415 | std::ostream& operator<<(std::ostream& os, Edge<T> e) {
      |               ^~~~~~~~
a.cc:153:15: note: 'template<class T> std::ostream& operator<<(std::ostream&, Edge<Cost>)' previously declared here
  153 | std::ostream& operator<<(std::ostream& os, Edge<T> e) {
      |               ^~~~~~~~
a.cc:422:10: error: redefinition of 'template<class T> T sq(T)'
  422 | inline T sq(T a) { return a * a; }
      |          ^~
a.cc:160:10: note: 'template<class T> T sq(T)' previously declared here
  160 | inline T sq(T a) { return a * a; }
      |          ^~
a.cc:425:10: error: redefinition of 'template<class T> T iceil(T, T)'
  425 | inline T iceil(T n, T d) { return (n + d - 1) / d; }
      |          ^~~~~
a.cc:163:10: note: 'template<class T> T iceil(T, T)' previously declared here
  163 | inline T iceil(T n, T d) { return (n + d - 1) / d; }
      |          ^~~~~
a.cc:428:3: error: redefinition of 'template<class T> T gcd(T, T)'
  428 | T gcd(T a, T b) {
      |   ^~~
a.cc:166:3: note: 'template<class T> T gcd(T, T)' previously declared here
  166 | T gcd(T a, T b) {
      |   ^~~
a.cc:437:3: error: redefinition of 'template<class T, class U> T ipow(T, U)'
  437 | T ipow(T b, U n) {
      |   ^~~~
a.cc:175:3: note: 'template<class T, class U> T ipow(T, U)' previously declared here
  175 | T ipow(T b, U n) {
      |   ^~~~
a.cc:449:10: error: redefinition of 'template<class T, class U> T kthbit(T, U)'
  449 | inline T kthbit(T a, U k) { return (a >> k) & 1; }
      |          ^~~~~~
a.cc:187:10: note: 'template<class T, class U> T kthbit(T, U)' previously declared here
  187 | inline T kthbit(T a, U k) { return (a >> k) & 1; }
      |          ^~~~~~
a.cc:452:10: error: redefinition of 'template<class T, class U> T mask(T, U)'
  452 | inline T mask(T a, U k) { return a & ((1 << k) - 1); }
      |          ^~~~
a.cc:190:10: note: 'template<class T, class U> T mask(T, U)' previously declared here
  190 | inline T mask(T a, U k) { return a & ((1 << k) - 1); }
      |          ^~~~
a.cc:455:18: error: redefinition of 'template<class T> std::map<T, int> compress(std::vector<_Tp>&)'
  455 | std::map<T, int> compress(std::vector<T>& v) {
      |                  ^~~~~~~~
a.cc:193:18: note: 'template<class T> std::map<T, int> compress(std::vector<_Tp>&)' previously declared here
  193 | std::map<T, int> compress(std::vector<T>& v) {
      |                  ^~~~~~~~
a.cc:465:3: error: redefinition of 'template<class T> T Vec(T)'
  465 | T Vec(T v) { return v; }
      |   ^~~
a.cc:203:3: note: 'template<class T> T Vec(T)' previously declared here
  203 | T Vec(T v) { return v; }
      |   ^~~
a.cc:468:6: error: redefinition of 'template<class T, class ... Ts> auto Vec(size_t, Ts ...)'
  468 | auto Vec(size_t l, Ts... ts) {
      |      ^~~
a.cc:206:6: note: 'template<class T, class ... Ts> auto Vec(size_t, Ts ...)' previously declared here
  206 | auto Vec(size_t l, Ts... ts) {
      |      ^~~
a.cc:482:5: error: redefinition of 'int main()'
  482 | int main() {
      |     ^~~~
a.cc:220:5: note: 'int main()' previously defined here
  220 | int main() {
      |     ^~~~