Problem p03358 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 114514191
#define MOD 1000000007
inline void chmax(int &x, int v) {
  if (x < v)
    x = v;
}

int N;
set<int> G[100000];
char C[100001];
bool dead[100000];
int W[100000];

void erase(int x) {
  dead[x] = true;
  for (int t : G[x]) {
    G[t].erase(x);
    if (G[t].size() == 1 && C[t] == 'B')
      erase(t);
  }
}
int ans = 0;
int dp0[100000], dp1[100000];
void dfs(int x, int p) {
  chmax(dp0[x], W[x]);
  chmax(dp1[x], 0);
  int maxdp0 = -INF, maxdp1 = -INF;
  for (int t : G[x])
    if (t != p) {
      dfs(t, x);
      chmax(ans, dp0[t]);
      chmax(dp0[x], dp0[t] + W[x]);
      chmax(dp1[x], dp1[t] + W[x]);
      chmax(ans, W[x] + maxdp0 + dp1[t]);
      chmax(ans, W[x] + maxdp1 + dp0[t]);
      chmax(maxdp0, dp0[t]);
      chmax(maxdp1, dp1[t]);
    }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].insert(y);
    G[y].insert(x);
  }
  cin >> C;
  rep(i, N) if (G[i].size() == 1 && C[i] == 'B') erase(i);
  int root = -1, V = 0;
  rep(i, N) if (!dead[i]) root = i, V++;
  if (root == -1) {
    cout << 0 << "\n";
    return 0;
  }
  int sum = 0;
  rep(i, N) if (!dead[i]) {
    W[i] = ((C[i] == 'B') ^ ((int)G[i].size() % 2)) == 0;
    sum += W[i];
  }
  int cost = 2 * (V - 1) + sum;
  rep(i, N) dp0[i] = dp1[i] = -INF;
  dfs(root, -1);
  cost -= 2 * ans;
  cout << cost << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 114514191
#define MOD 1000000007
inline void chmax(int &x, int v) {
  if (x < v)
    x = v;
}

int N;
set<int> G[100000];
char C[100001];
bool dead[100000];
int W[100000];

void erase(int x) {
  dead[x] = true;
  for (int t : G[x])
    if (!dead[t]) {
      G[t].erase(x);
      if (G[t].size() == 1 && C[t] == 'B')
        erase(t);
    }
}
int ans = 0;
int dp0[100000], dp1[100000];
void dfs(int x, int p) {
  chmax(dp0[x], W[x]);
  chmax(dp1[x], 0);
  int maxdp0 = -INF, maxdp1 = -INF;
  for (int t : G[x])
    if (t != p) {
      dfs(t, x);
      chmax(ans, dp0[t]);
      chmax(dp0[x], dp0[t] + W[x]);
      chmax(dp1[x], dp1[t] + W[x]);
      chmax(ans, W[x] + maxdp0 + dp1[t]);
      chmax(ans, W[x] + maxdp1 + dp0[t]);
      chmax(maxdp0, dp0[t]);
      chmax(maxdp1, dp1[t]);
    }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, N - 1) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].insert(y);
    G[y].insert(x);
  }
  cin >> C;
  rep(i, N) if (G[i].size() == 1 && C[i] == 'B') erase(i);
  int root = -1, V = 0;
  rep(i, N) if (!dead[i]) root = i, V++;
  if (root == -1) {
    cout << 0 << "\n";
    return 0;
  }
  int sum = 0;
  rep(i, N) if (!dead[i]) {
    W[i] = ((C[i] == 'B') ^ ((int)G[i].size() % 2)) == 0;
    sum += W[i];
  }
  int cost = 2 * (V - 1) + sum;
  rep(i, N) dp0[i] = dp1[i] = -INF;
  dfs(root, -1);
  cost -= 2 * ans;
  cout << cost << "\n";
  return 0;
}