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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

const int MAX_N = 100010;
int par[MAX_N];
int myrank[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    myrank[i] = 0;
  }
}

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;

  // if(myrank[x]<myrank[y]){
  //	par[x]=y;
  // }else{
  par[y] = x;
  // if(myrank[x]==myrank[y])myrank[x]++;
  //}
}

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

int main() {
  int n, q;
  while (cin >> n >> q, n | q) {
    vi a(n);
    vi parents(n, -1);
    REP(i, n - 1) {
      int p;
      cin >> p;
      parents[i + 1] = p - 1;
    }

    init(n);
    vi marked(n);

    vector<pair<char, int>> queries;
    REP(i, q) {
      char c;
      int v;
      cin >> c >> v;
      bool add = true;
      if (c == 'M') {
        if (marked[v - 1]) {
          add = false;
        } else {
          marked[v - 1] = 1;
        }
      }
      if (add) {
        queries.push_back(make_pair(c, v - 1));
      }
    }

    REP(i, n) {
      if (!marked[i] && parents[i] != -1) {
        unite(parents[i], i);
      }
    }

    ll ans = 0;
    REP(i, queries.size()) {
      char c = queries[queries.size() - i - 1].first;
      int v = queries[queries.size() - i - 1].second;
      if (c == 'Q') {
        ans += find(v) + 1;
      } else if (v != 0) {
        unite(parents[v], v);
      }
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

const int MAX_N = 100000;
int par[MAX_N];
int myrank[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    myrank[i] = 0;
  }
}

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;

  if (myrank[x] < myrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (myrank[x] == myrank[y])
      myrank[x]++;
  }
}

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

int main() {
  int n, q;
  while (cin >> n >> q, n | q) {
    vi a(n);
    vi parents(n, -1);
    REP(i, n - 1) {
      int p;
      cin >> p;
      parents[i + 1] = p - 1;
    }

    init(n);
    vi marked(n);

    vector<pair<char, int>> queries;
    REP(i, q) {
      char c;
      int v;
      cin >> c >> v;
      queries.push_back(make_pair(c, v - 1));
      if (c == 'M') {
        marked[v - 1] = 1;
      }
    }

    REP(i, n) {
      if (!marked[i] && parents[i] != -1) {
        unite(parents[i], i);
      }
    }

    ll ans = 0;
    REP(i, q) {
      char c = queries[q - i - 1].first;
      int v = queries[q - i - 1].second;
      if (c == 'Q') {
        ans += find(i) + 1;
      } else {
        unite(parents[i], i);
      }
    }

    cout << ans << endl;
  }
}