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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
int par[100000];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  par[x] = y;
}
int main() {
  int n, q;
  while (cin >> n >> q, n) {
    init(n);
    int par0[100000] = {};
    int marked[100000] = {};
    marked[0] = 1;
    int t;
    rep(i, n - 1) {
      cin >> t;
      t--;
      par0[i + 1] = t;
    }
    vector<pair<char, int>> p;
    char a;
    int b;
    rep(i, q) {
      cin >> a >> b;
      b--;
      if (a == 'M')
        marked[b] += 1;
      p.push_back({a, b});
    }
    rep(i, n) {
      if (marked[i] == 0) {
        unite(i, par0[i]);
      }
    }
    ll out = 0;
    for (int i = q - 1; i >= 0; i--) {
      if (p[i].first == 'M') {
        marked[p[i].second]--;
        if (marked[p[i].second] == 0) {
          unite(p[i].second, par0[p[i].second]);
        }
      } else {
        out += (ll)(find(p[i].second) + 1);
      }
    }
    cout << out << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
int par[100000];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  par[x] = y;
}
int main() {
  int n, q;
  while (cin >> n >> q, n) {
    init(n);
    rep(i, n - 1) { cin >> par[i + 1]; }
    vector<pair<char, int>> p;
    int marked[100001] = {};
    marked[0] = 1;
    char a;
    int b;
    rep(i, q) {
      cin >> a >> b;
      b--;
      if (a == 'M')
        marked[b] = 1;
      p.push_back({a, b});
    }
    ll out = 0;
    rep(i, n) {
      if (!marked[i]) {
        unite(i, par[i]);
      }
    }
    for (int i = q - 1; i >= 0; i--) {
      if (p[i].first == 'M') {
        unite(p[i].second, par[p[i].second]);
      } else {
        out += (ll)find(p[i].second) + (ll)1;
      }
    }
    cout << out << endl;
  }
  return 0;
}