Problem p03358 (C++)

Buggy submission (status: Runtime Error):
// I need some holiday
// But I don't wish that everyday is holiday
// Because I didn't bear the pain
// Because it wasn't a true freedom
//         ——『イヤホンと蝉時雨』·Orangestar

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define MP make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline int chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline int chkmax(T &a, const T &b) {
  return b > a ? a = b, 1 : 0;
}
template <typename T> inline T sqr(const T &val) { return val * val; }

namespace fastIO {
const int MAX_BUFFER_SIZE = 1 << 16;

char buffer[MAX_BUFFER_SIZE], *cur = buffer, *ed = buffer;

inline char getc() {
  return cur == ed && (ed = (cur = buffer) +
                            fread(buffer, 1, MAX_BUFFER_SIZE, stdin),
                       cur == ed)
             ? EOF
             : *cur++;
}
} // namespace fastIO
using fastIO::getc;

template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getc());
  for (; !isdigit(ch); ch = getc())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getc())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

inline char *read_str(char *s) {
  register char ch(getc());
  while (!isgraph(ch))
    ch = getc();
  for (; isgraph(ch); ch = getc())
    *s++ = ch;
  *s = '\0';
  return s;
}

// END tpl.

inline char safe_getc() {
  register char ch(getc());
  while (!isgraph(ch))
    ch = getc();
  return ch;
}

const int MAXN = (int)1e5;

int n;

vector<int> adj[MAXN + 5];

int col[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i < n; ++i) {
    int u = read<int>(), v = read<int>();
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    col[i] = safe_getc() == 'B';
}

vector<int> V, G[MAXN + 5];

int dep[MAXN + 5], val[MAXN + 5];

inline int dfs(int u, int fa) {
  dep[u] = dep[fa] + val[u];

  int p = u;
  for (auto v : adj[u])
    if (v != fa) {
      int q = dfs(v, u);
      if (dep[q] > dep[p])
        p = q;
    }
  return p;
}

inline void solve() {
  static queue<int> q;
  static int ban[MAXN + 5], deg[MAXN + 5];

  for (int i = 1; i <= n; ++i)
    if ((deg[i] = SZ(adj[i])) == 1 && col[i] == 1)
      ban[i] = 1, q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();

    for (auto v : adj[u])
      if (!ban[v] && --deg[v] == 1 && col[v] == 1)
        ban[v] = 1, q.push(v);
  }

  V.clear();
  for (int u = 1; u <= n; ++u)
    if (!ban[u]) {
      V.push_back(u);
      for (auto v : adj[u])
        if (!ban[v])
          G[u].push_back(v);
    }

  int ans = 2 * (SZ(V) - 1);
  for (auto i : V) {
    col[i] ^= SZ(G[i]) & 1;
    val[i] = col[i] == 0;
    ans += col[i] == 0;
  }

  int S = dfs(V[0], 0);
  int T = dfs(S, 0);
  printf("%d\n", max(0, ans - 2 * dep[T]));
}

int main() {
#ifdef K_ON
  freopen("F.in", "r", stdin);
  freopen("F.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
// I need some holiday
// But I don't wish that everyday is holiday
// Because I didn't bear the pain
// Because it wasn't a true freedom
//         ——『イヤホンと蝉時雨』·Orangestar

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define MP make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline int chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline int chkmax(T &a, const T &b) {
  return b > a ? a = b, 1 : 0;
}
template <typename T> inline T sqr(const T &val) { return val * val; }

namespace fastIO {
const int MAX_BUFFER_SIZE = 1 << 16;

char buffer[MAX_BUFFER_SIZE], *cur = buffer, *ed = buffer;

inline char getc() {
  return cur == ed && (ed = (cur = buffer) +
                            fread(buffer, 1, MAX_BUFFER_SIZE, stdin),
                       cur == ed)
             ? EOF
             : *cur++;
}
} // namespace fastIO
using fastIO::getc;

template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getc());
  for (; !isdigit(ch); ch = getc())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getc())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

inline char *read_str(char *s) {
  register char ch(getc());
  while (!isgraph(ch))
    ch = getc();
  for (; isgraph(ch); ch = getc())
    *s++ = ch;
  *s = '\0';
  return s;
}

// END tpl.

inline char safe_getc() {
  register char ch(getc());
  while (!isgraph(ch))
    ch = getc();
  return ch;
}

const int MAXN = (int)1e5;

int n;

vector<int> adj[MAXN + 5];

int col[MAXN + 5];

inline void input() {
  n = read<int>();
  for (int i = 1; i < n; ++i) {
    int u = read<int>(), v = read<int>();
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    col[i] = safe_getc() == 'B';
}

vector<int> V, G[MAXN + 5];

int dep[MAXN + 5], val[MAXN + 5];

inline int dfs(int u, int fa) {
  dep[u] = dep[fa] + val[u];

  int p = u;
  for (auto v : adj[u])
    if (v != fa) {
      int q = dfs(v, u);
      if (dep[q] > dep[p])
        p = q;
    }
  return p;
}

inline void solve() {
  static queue<int> q;
  static int ban[MAXN + 5], deg[MAXN + 5];

  for (int i = 1; i <= n; ++i)
    if ((deg[i] = SZ(adj[i])) == 1 && col[i] == 1)
      ban[i] = 1, q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();

    for (auto v : adj[u])
      if (!ban[v] && --deg[v] == 1 && col[v] == 1)
        ban[v] = 1, q.push(v);
  }

  V.clear();
  for (int u = 1; u <= n; ++u)
    if (!ban[u]) {
      V.push_back(u);
      for (auto v : adj[u])
        if (!ban[v])
          G[u].push_back(v);
    }

  if (SZ(V) == 0) {
    puts("0");
    return;
  }
  if (SZ(V) == 1) {
    printf("%d\n", col[V[0]] == 0);
    return;
  }

  int ans = 2 * (SZ(V) - 1);
  for (auto i : V) {
    col[i] ^= SZ(G[i]) & 1;
    val[i] = col[i] == 0;
    ans += col[i] == 0;
  }

  int S = dfs(V[0], 0);
  int T = dfs(S, 0);
  printf("%d\n", max(0, ans - 2 * dep[T]));
}

int main() {
#ifdef K_ON
  freopen("F.in", "r", stdin);
  freopen("F.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}