Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, q, p[100000];
int m[100000], v[100000];
char type[100000];
vector<vi> e;

int pa[100000];
int root(int x) {
  if (x == pa[x])
    return x;
  return pa[x] = root(pa[x]);
}

int main() {
  while (scanf("%d%d", &n, &q), n) {
    e.clear();
    e.resize(n);
    memset(m, 0, sizeof(m));

    rep(i, n - 1) {
      scanf("%d", p + i + 1), p[i + 1]--;
      e[i + 1].pb(p[i + 1]);
      e[p[i + 1]].pb(i + 1);
    }
    rep(i, q) {
      scanf(" %c%d", type + i, v + i);
      v[i]--;
      if (type[i] == 'M')
        m[v[i]]++;
    }
    m[0] = 1;

    queue<pi> Q;
    rep(i, n) {
      pa[i] = i;
      if (m[i])
        Q.push(mp(i, i));
    }

    while (!Q.empty()) {
      int c = Q.front().first, r = Q.front().second;
      Q.pop();
      rep(i, e[c].size()) if (e[c][i] != p[c] && !m[e[c][i]]) {
        pa[e[c][i]] = r;
        Q.push(mp(e[c][i], r));
      }
    }
    ll ans = 0;
    for (int i = q - 1; i >= 0; i--) {
      if (type[i] == 'M') {
        if (--m[v[i]] == 0)
          pa[v[i]] = root(p[v[i]]);
      } else
        ans += root(v[i]) + 1;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, q, p[100000];
int m[100000], v[100000];
char type[100000];
vector<vi> e;

int pa[100000];
int root(int x) {
  if (x == pa[x])
    return x;
  return pa[x] = root(pa[x]);
}

int main() {
  while (scanf("%d%d", &n, &q), n) {
    e.clear();
    e.resize(n);
    memset(m, 0, sizeof(m));

    rep(i, n - 1) {
      scanf("%d", p + i + 1), p[i + 1]--;
      e[i].pb(p[i + 1]);
      e[p[i + 1]].pb(i);
    }
    rep(i, q) {
      scanf(" %c%d", type + i, v + i);
      v[i]--;
      if (type[i] == 'M')
        m[v[i]]++;
    }
    m[0] = 1;

    queue<pi> Q;
    rep(i, n) {
      pa[i] = i;
      if (m[i])
        Q.push(mp(i, i));
    }
    while (!Q.empty()) {
      int c = Q.front().first, r = Q.front().second;
      Q.pop();
      rep(i, e[c].size()) if (!m[e[c][i]]) {
        pa[e[c][i]] = r;
        Q.push(mp(e[c][i], r));
      }
    }
    ll ans = 0;
    for (int i = q - 1; i >= 0; i--) {
      if (type[i] == 'M') {
        if (--m[v[i]] == 0)
          pa[v[i]] = root(p[v[i]]);
      } else
        ans += root(v[i]) + 1;
    }
    cout << ans << endl;
  }
  return 0;
}