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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int par[100000];
int mark[100000];
int uf[100000];
int root(int x) {
  if (uf[x] == x)
    return x;
  return uf[x] = root(uf[x]);
}
int main() {
  int N, Q;
  while (cin >> N >> Q && N) {
    par[0] = 0;
    uf[0] = 0;
    mark[0] = 1;
    REP(i, N - 1) {
      cin >> par[i + 1];
      par[i + 1]--;
      uf[i + 1] = par[i + 1];
    }
    memset(mark, 0, sizeof(mark));
    char c[100000];
    int v[100000];
    REP(i, Q) {
      cin >> c[i] >> v[i];
      v[i]--;
      if (c[i] == 'M') {
        mark[v[i]]++;
      }
    }
    REP(i, N) if (mark[i] > 0) { uf[i] = i; }
    ll ans = 0;
    for (int i = Q - 1; i >= 0; i--) {
      if (c[i] == 'M') {
        mark[v[i]]--;
        if (mark[v[i]] == 0) {
          uf[v[i]] = par[v[i]];
        }
      } else {
        ans += root(v[i]) + 1;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int par[100000];
int mark[100000];
int uf[100000];
int root(int x) {
  if (uf[x] == x)
    return x;
  return uf[x] = root(uf[x]);
}
int main() {
  int N, Q;
  while (cin >> N >> Q && N) {
    par[0] = -1;
    uf[0] = 0;
    REP(i, N - 1) {
      cin >> par[i + 1];
      par[i + 1]--;
      uf[i + 1] = par[i + 1];
    }
    memset(mark, 0, sizeof(mark));
    char c[100000];
    int v[100000];
    REP(i, Q) {
      cin >> c[i] >> v[i];
      v[i]--;
      if (c[i] == 'M') {
        mark[v[i]]++;
      }
    }
    REP(i, N) if (mark[i] > 0) { uf[i] = i; }
    ll ans = 0;
    for (int i = Q - 1; i >= 0; i--) {
      if (c[i] == 'M') {
        mark[v[i]]--;
        if (mark[v[i]] == 0) {
          uf[v[i]] = par[v[i]];
        }
      } else {
        ans += root(v[i]) + 1;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}