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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
typedef pair<char, int> cp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX

int n, m;

vector<int> vs[100005];
int uni[100010];
int ma[100005];
int marked[100005];

void dfs(int no, int p) {
  ma[no] = p;
  if (marked[no] >= 1)
    p = no;
  else
    uni[no] = p;

  rep(i, vs[no].size()) { dfs(vs[no][i], p); }
}

int find(int p) {
  if (uni[p] == p)
    return p;
  uni[p] = find(uni[p]);
  return uni[p];
}

int main(void) {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;
    reg(i, 1, n) {
      vs[i].clear();
      uni[i] = i;
    }
    memset(marked, 0, sizeof(marked));

    reg(i, 2, n) {
      int a;
      scanf("%d", &a);
      vs[a].push_back(i);
    }

    vector<cp> qs;
    rep(i, m) {
      int a;
      char s[5];
      scanf("%s%d", s, &a);
      if (s[0] == 'M')
        marked[a]++;
      qs.push_back(cp(s[0], a));
    }

    marked[1] = 1;
    dfs(1, 1);

    lli ans = 0;
    irep(i, qs.size()) {
      int a = qs[i].sec;
      if (qs[i].fir == 'Q') {
        lli na = find(a);
        ans += na;
      } else {
        marked[a]--;
        if (marked[a] > 0)
          continue;
        // if(find(a) == find(ma[a]))continue;
        uni[a] = find(ma[a]);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<char, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX

vector<int> vs[100005];
lli ps[100005];
int ic[100005];

struct unifo {
  int un;
  lli uni[100005];
  void init(int n) {
    rep(i, n) uni[i] = i;
    un = n;
  }
  lli find(int p) {
    if (uni[p] == p)
      return p;
    else
      return (uni[p] = find(uni[p]));
  }
  void mer(int p, int q) { uni[find(p)] = find(q); }
  bool same(int p, int q) { return find(p) == find(q); }
};

unifo uni;

void dfs(int no, int p) {
  ps[no] = p;
  // printf("%d %d\\n",no,p);
  if (ic[no] == 1)
    p = no;
  else
    uni.mer(no, p);
  rep(i, vs[no].size()) { dfs(vs[no][i], p); }
}

int n, m;
int main(void) {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;
    uni.init(n + 3);

    rep(i, n) {
      vs[i].clear();
      ps[i] = 0;
    }

    rep(i, n - 1) {
      int a;
      scanf("%d", &a);
      vs[a].push_back(i + 2);
    }

    vector<mp> qs;
    rep(i, m) {
      int a;
      char c;
      scanf("%s%d", &c, &a);
      if (c == 'M')
        ic[a] = 1;
      qs.push_back(mp(c, a));
    }

    dfs(1, 1);

    lli ans = 0;
    irep(i, qs.size()) {
      int a = qs[i].sec;
      if (qs[i].fir == 'Q') {
        ans += uni.find(ps[a]);
        // printf("%lld\\n",uni.find(ps[a]));
      } else {
        uni.mer(uni.find(a), uni.find(ps[a]));
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}