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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const ll MOD = 1e9 + 7;
const int inf = (ll)1e9;
const double PI = acos(-1.0);

struct UF {
  int par[100001];
  bool mark[100001];
  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = 0;
      mark[i] = 0;
    }
    mark[0] = 1;
  }
  void parent(int p, int c) { par[c] = p; }
  void Mark(int a) { mark[a] = 1; }
  int find(int a) {
    if (mark[a])
      return a;
    else
      return find(par[a]);
  }
  void test(int n) {
    for (int i = 0; i < n; i++) {
      cerr << par[i] << " ";
    }
    cerr << endl;
    for (int i = 0; i < n; i++) {
      cerr << mark[i] << " ";
    }
    cerr << endl;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      return 0;
    UF uf;
    uf.init(n);
    for (int i = 1; i < n; i++) {
      int a;
      cin >> a;
      a--;
      uf.parent(a, i);
    }
    ll ans = 0;
    for (int i = 0; i < m; i++) {
      char a;
      int b;
      cin >> a >> b;
      b--;
      if (a == 'M') {
        uf.Mark(b);
        // uf.test(n);
      } else {
        ans += uf.find(b) + 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const ll MOD = 1e9 + 7;
const int inf = (ll)1e9;
const double PI = acos(-1.0);
const int N = 100000;
struct UF {
  static const int n = 100001;
  int par[100001];
  int Rank[100001];
  void init(int size) {
    for (int i = 0; i < size; i++) {
      par[i] = i;
      Rank[i] = 0;
    }
  }
  int find(int a) {
    if (par[a] == a)
      return a;
    return par[a] = find(par[a]);
  }
  bool same(int a, int b) { return find(a) == find(b); }
  void unite(int a, int b) {
    int x = find(a);
    int y = find(b);
    if (x == y)
      return;
    if (Rank[x] > Rank[y]) {
      par[y] = x;
    } else if (Rank[x] < Rank[y]) {
      par[x] = y;
    } else {
      if (x > y) {
        par[x] = y;
        Rank[y]++;
      } else {
        par[y] = x;
        Rank[x]++;
      }
    }
  }
  void test(int size) {
    cerr << "==================" << endl;
    for (int i = 0; i < size; i++) {
      cerr << par[i] << " ";
    }
    cerr << "\\n";
    for (int i = 0; i < size; i++) {
      cerr << Rank[i] << " ";
    }
    cerr << "\\n";
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> edge[n];
    for (int i = 1; i < n; i++) {
      int a;
      cin >> a;
      a--;
      edge[a].push_back(i);
    }
    UF uf;
    uf.init(n);
    vector<pair<int, int>> Q, M;
    int nowq = 0;
    bool flag1 = 0;
    M.push_back(make_pair(-1, 0));
    for (int i = 0; i < m; i++) {
      char a;
      int b;
      cin >> a >> b;
      b--;
      if (a == 'Q') {
        if (flag1)
          nowq++;
        Q.push_back(make_pair(nowq, b));
        flag1 = 0;
      } else {
        M.push_back(make_pair(nowq, b));
        flag1 = 1;
      }
    }
    ll ans = 0;
    int qsize = (int)Q.size();
    int msize = (int)M.size();
    sort(Q.begin(), Q.end());
    int nowqq = Q[qsize - 1].fi;
    int nowmm = M[msize - 1].fi;
    int lastq = qsize - 1;
    int lastm = msize - 1;
    bool flag = 1;
    while (flag) {
      flag = 0;
      int lastmfi = M[lastm].fi;
      for (int i = lastm; i >= 0; i--) {
        if (lastmfi > M[i].fi)
          break;
        flag = 1;
        for (int j = 0; j < (int)edge[M[i].se].size(); j++) {
          // cerr<<"unite:"<<M[i].se<<" "<<edge[M[i].se][j]<<endl;
          uf.unite(M[i].se, edge[M[i].se][j]);
        }
        lastm--;
      }
      int lastqfi = Q[lastq].fi;
      for (int i = lastq; i >= 0; i--) {
        if (lastqfi > Q[i].fi)
          break;
        flag = 1;
        int aa = (uf.find(Q[i].se) + 1);
        // cerr<<aa<<endl;
        ans += aa;
        // uf.test(n);
        lastq--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}