Problem p03358 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
template <bool Directed, typename TEdge, bool Index> struct graph {
  using EType = TEdge;
  vector<TEdge> edges;
  vector<vector<int>> adj;
  graph(int n) : adj(n + Index) {}
  graph(int n, int m) : graph(n) { edges.reserve(m << not Directed); }
  TEdge &operator()(int e) { return edges[e]; }
  vector<int> &operator[](int u) { return adj[u]; }
  int size() { return adj.size() - Index; }
  void append(int u, const TEdge &e) {
    adj[u].push_back(edges.size());
    edges.push_back(e);
  }
  void add_edge(const TEdge &e) {
    append(e.u, e);
    if (not Directed)
      append(e.v, e.reverse());
  }
};
struct edge {
  int u, v;
  edge reverse() const { return {v, u}; }
  friend ostream &operator<<(ostream &os, const edge &e) {
    return os << "{u: " << e.u << ", v: " << e.v << "}";
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  graph<0, edge, 1> g(n, n - 1);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g.add_edge({u, v});
  }
  string c;
  cin >> c;
  c.insert(c.begin(), ' ');
  vector<int> degree(n + 1), degree_parity(n + 1);
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if ((degree[i] = g[i].size()) == 1 and c[i] == 'B') {
      q.push(i);
    }
    degree_parity[i] = degree[i] & 1;
  }
  vector<bool> deleted(n + 1);
  deleted[0] = true;
  int edges = n - 1;
  while (not q.empty()) {
    int u = q.front();
    q.pop();
    deleted[u] = true;
    edges--;
    for (int e : g[u]) {
      degree_parity[g(e).v] ^= 1;
      if (--degree[g(e).v] == 1 and c[g(e).v] == 'B') {
        q.push(g(e).v);
      }
    }
  }
  if (count(deleted.begin(), deleted.end(), false) == 1) {
    cout << (c[find(deleted.begin(), deleted.end(), false) - deleted.begin()] ==
             'W')
         << endl;
  } else {
    auto flip_cost = [&](int i) { return (c[i] == 'W') ^ degree_parity[i]; };
    auto farthest = [&](int s) -> pair<int, int> {
      vector<int> dist(n + 1, -1);
      priority_queue<pair<int, int>, vector<pair<int, int>>,
                     greater<pair<int, int>>>
          q;
      q.push({dist[s] = 0, s});
      while (not q.empty()) {
        int d, u;
        tie(d, u) = q.top();
        q.pop();
        for (int e : g[u]) {
          if (not deleted[g(e).v] and dist[g(e).v] == -1) {
            q.push({dist[g(e).v] = d + 1 + flip_cost(g(e).v) -
                                   (flip_cost(g(e).v) ^ (degree[g(e).v] > 1)),
                    g(e).v});
          }
        }
      }
      return {max_element(dist.begin(), dist.end()) - dist.begin(),
              *max_element(dist.begin(), dist.end())};
    };
    int cost = -2;
    for (int i = 1; i <= n; i++) {
      if (not deleted[i]) {
        cost += flip_cost(i) + 2;
      }
    }
    cout << cost -
                farthest(farthest(find(deleted.begin(), deleted.end(), false) -
                                  deleted.begin())
                             .first)
                    .second +
                1
         << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
template <bool Directed, typename TEdge, bool Index> struct graph {
  using EType = TEdge;
  vector<TEdge> edges;
  vector<vector<int>> adj;
  graph(int n) : adj(n + Index) {}
  graph(int n, int m) : graph(n) { edges.reserve(m << not Directed); }
  TEdge &operator()(int e) { return edges[e]; }
  vector<int> &operator[](int u) { return adj[u]; }
  int size() { return adj.size() - Index; }
  void append(int u, const TEdge &e) {
    adj[u].push_back(edges.size());
    edges.push_back(e);
  }
  void add_edge(const TEdge &e) {
    append(e.u, e);
    if (not Directed)
      append(e.v, e.reverse());
  }
};
struct edge {
  int u, v;
  edge reverse() const { return {v, u}; }
  friend ostream &operator<<(ostream &os, const edge &e) {
    return os << "{u: " << e.u << ", v: " << e.v << "}";
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  graph<0, edge, 1> g(n, n - 1);
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g.add_edge({u, v});
  }
  string c;
  cin >> c;
  c.insert(c.begin(), ' ');
  vector<int> degree(n + 1), degree_parity(n + 1);
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if ((degree[i] = g[i].size()) == 1 and c[i] == 'B') {
      q.push(i);
    }
    degree_parity[i] = degree[i] & 1;
  }
  vector<bool> deleted(n + 1);
  deleted[0] = true;
  int edges = n - 1;
  while (not q.empty()) {
    int u = q.front();
    q.pop();
    deleted[u] = true;
    edges--;
    for (int e : g[u]) {
      degree_parity[g(e).v] ^= 1;
      if (--degree[g(e).v] == 1 and c[g(e).v] == 'B') {
        q.push(g(e).v);
      }
    }
  }
  if (count(deleted.begin(), deleted.end(), false) == 0) {
    cout << 0 << endl;
  } else if (count(deleted.begin(), deleted.end(), false) == 1) {
    cout << (c[find(deleted.begin(), deleted.end(), false) - deleted.begin()] ==
             'W')
         << endl;
  } else {
    auto flip_cost = [&](int i) { return (c[i] == 'W') ^ degree_parity[i]; };
    auto farthest = [&](int s) -> pair<int, int> {
      vector<int> dist(n + 1, -1);
      priority_queue<pair<int, int>, vector<pair<int, int>>,
                     greater<pair<int, int>>>
          q;
      q.push({dist[s] = 0, s});
      while (not q.empty()) {
        int d, u;
        tie(d, u) = q.top();
        q.pop();
        for (int e : g[u]) {
          if (not deleted[g(e).v] and dist[g(e).v] == -1) {
            q.push({dist[g(e).v] = d + 1 + flip_cost(g(e).v) -
                                   (flip_cost(g(e).v) ^ (degree[g(e).v] > 1)),
                    g(e).v});
          }
        }
      }
      return {max_element(dist.begin(), dist.end()) - dist.begin(),
              *max_element(dist.begin(), dist.end())};
    };
    int cost = -2;
    for (int i = 1; i <= n; i++) {
      if (not deleted[i]) {
        cost += flip_cost(i) + 2;
      }
    }
    cout << cost -
                farthest(farthest(find(deleted.begin(), deleted.end(), false) -
                                  deleted.begin())
                             .first)
                    .second +
                1
         << endl;
  }
  return 0;
}