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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int lnt;
typedef pair<lnt, lnt> P;
lnt n, q;
lnt queri[100000][2];
bool marked[100000];
lnt par[100000];
void init(void) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}

lnt find(lnt x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}

bool same(lnt x, lnt y) { return find(x) == find(y); }

void unite(lnt x, lnt y) {
  x = find(x);
  y = find(y);
  par[x] = y;
  return;
}

char str[10];
lnt anc[100000];
lnt m;

int main() {
  while (1) {
    lnt ans = 0;
    scanf("%lld%lld", &n, &q);
    init();
    if (!n && !q)
      return 0;
    memset(marked, 0, sizeof(marked));
    for (lnt i = 1; i < n; i++) {
      scanf("%lld", &anc[i]);
      anc[i]--;
    }
    marked[0] = 1;
    for (lnt i = 0; i < q; i++) {
      scanf("%s%lld", str, &queri[i][1]);
      queri[i][1]--;
      if (*str == 'M') {
        queri[i][0] = 1;
        if (!marked[queri[i][1]])
          queri[i][0] = 2;
        marked[queri[i][1]] = 1;
      } else
        queri[i][0] = 0;
    }
    for (lnt i = 0; i < n; i++) {
      m = i;
      while (!marked[m] && !same(m, anc[m])) {
        unite(m, anc[m]);
        m = anc[m];
      }
    }
    for (lnt i = q - 1; i >= 0; i--) {
      if (queri[i][0]) {
        if (queri[i][0] == 2)
          unite(queri[i][1], anc[queri[i][1]]);
      } else {
        m = queri[i][1];
        ans += (find(m) + 1);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int lnt;
typedef pair<lnt, lnt> P;
lnt n, q;
lnt queri[100000][2];
bool marked[100000];
lnt par[100000];
void init(void) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}

lnt find(lnt x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}

bool same(lnt x, lnt y) { return find(x) == find(y); }

void unite(lnt x, lnt y) {
  x = find(x);
  y = find(y);
  par[x] = y;
  return;
}

char str[10];
lnt anc[100000];
lnt depth[100000];
vector<lnt> to[100000];
lnt m;

void defdep(lnt x, lnt y) {
  depth[x] = y;
  for (lnt i = 0; i < to[x].size(); i++)
    defdep(to[x][i], y + 1);
}

int main() {
  while (1) {
    lnt ans = 0;
    scanf("%lld%lld", &n, &q);
    if (!n && !q)
      return 0;
    memset(marked, 0, sizeof(marked));
    for (lnt i = 1; i < n; i++) {
      scanf("%lld", &anc[i]);
      anc[i]--;
      to[anc[i]].push_back(i);
    }
    defdep(0, 0);
    marked[0] = 1;
    for (lnt i = 0; i < q; i++) {
      scanf("%s%lld", str, &queri[i][1]);
      queri[i][1]--;
      if (*str == 'M') {
        queri[i][0] = 1;
        marked[queri[i][1]] = 1;
      } else
        queri[i][0] = 0;
    }
    for (lnt i = 0; i < n; i++) {
      m = i;
      while (!marked[m] && !same(m, anc[m])) {
        unite(m, anc[m]);
        m = anc[m];
      }
    }
    for (lnt i = q - 1; i >= 0; i--) {
      if (queri[i][0]) {
        unite(queri[i][1], anc[queri[i][1]]);
      } else {
        m = queri[i][1];
        ans += depth[m] - depth[find(m)];
      }
    }
    cout << ans << endl;
  }
}