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

//
//  main.cpp
//

#include <iostream>
// #include <algorithm>
#include <vector>
// #include <limits>
#include <math.h>
#include <queue>
// #include <tuple>
// #include <stdio.h>
// #include <stdlib.h>
// #include <string>
// #include "string.h"
// #include <unordered_map>
// #include <unordered_set>
// #include <array>
// #include <set>

using namespace std;

using ll_t = long long;
// #define ll_t long long

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2170&lang=jp

class MarkedTree {
public:
  vector<size_t> parent;
  vector<bool> marks;
  explicit MarkedTree(size_t size) {
    parent = vector<size_t>(size, 0);
    marks = vector<bool>(size, false);
    parent[0] = -1; // root
    marks[0] = true;
  }
  size_t findMarkedParent(size_t node) {
    if (marks[node]) {
      return node;
    } else {
      size_t ret = findMarkedParent(parent[node]);
      parent[node] = ret;
      return ret;
    }
  }
  void unMark(size_t node) { marks[node] = false; }
  void mark(size_t node) { marks[node] = true; }
  void setParent(size_t node, size_t parentNode) { parent[node] = parentNode; }
};

int main() {
  while (true) {
    int N, Q;
    scanf("%d %d", &N, &Q);
    if (N == 0 && Q == 0) {
      return 0;
    }
    MarkedTree tree(N);
    for (int node = 1; node <= N - 1; node++) {
      int parent;
      scanf("%d", &parent);
      tree.setParent(node, parent - 1);
    }
    vector<pair<char, int>> inputs;
    inputs.reserve(Q);
    for (int i = 0; i < Q; i++) {
      char q;
      int p;
      scanf(" %c", &q);
      scanf("%d", &p);
      if (q == 'M') {
        if (!tree.marks[p - 1]) {
          inputs.emplace_back(q, p);
        }
        tree.mark(p - 1);
      } else if (q == 'Q') {
        inputs.emplace_back(q, p);
      }
    }
    ll_t querySum = 0;
    for (int i = (int)inputs.size() - 1; i >= 0; i--) {
      char q = inputs[i].first;
      int node = inputs[i].second;
      if (q == 'Q') {
        querySum += tree.findMarkedParent(node - 1) + 1;
      } else { // M
        tree.unMark(node - 1);
      }
    }
    cout << querySum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

//
//  main.cpp
//

#include <iostream>
// #include <algorithm>
#include <vector>
// #include <limits>
#include <math.h>
#include <queue>
// #include <tuple>
// #include <stdio.h>
// #include <stdlib.h>
// #include <string>
// #include "string.h"
// #include <unordered_map>
// #include <unordered_set>
// #include <array>
// #include <set>

using namespace std;

using ll_t = long long;
// #define ll_t long long

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2170&lang=jp

class UnionFind {
protected:
  vector<size_t> parent;
  vector<size_t> rank;

public:
  explicit UnionFind(size_t size) {
    parent.resize(size);
    rank.resize(size);
    for (size_t i = 0; i < size; i++) {
      parent[i] = i;
      rank[i] = 0;
    }
  }
  virtual ~UnionFind() {}
  void unite(size_t x, size_t y) {
    size_t px = findParent(x);
    size_t py = findParent(y);
    if (px == py) {
      return;
    }
    if (rank[px] < rank[py]) {
      parent[px] = py;
    } else if (rank[px] > rank[py]) {
      parent[py] = px;
    } else { // rank[px] == rank[py]
      parent[py] = px;
      rank[px]++;
    }
  }
  bool isSame(size_t x, size_t y) { return findParent(x) == findParent(y); }
  size_t findParent(size_t x) {
    if (parent[x] == x) {
      return x;
    } else {
      parent[x] = findParent(parent[x]);
      return parent[x];
    }
  }
};

class MarkedTree {
public:
  vector<size_t> parent;
  vector<bool> marks;
  explicit MarkedTree(size_t size) {
    parent = vector<size_t>(size, 0);
    marks = vector<bool>(size, false);
    parent[0] = -1; // root
    marks[0] = true;
  }
  size_t findMarkedParent(size_t node) {
    if (marks[node]) {
      return node;
    } else {
      size_t ret = findMarkedParent(parent[node]);
      parent[node] = ret;
      return ret;
    }
  }
  void unMark(size_t node) { marks[node] = false; }
  void mark(size_t node) { marks[node] = true; }
  void setParent(size_t node, size_t parentNode) { parent[node] = parentNode; }
};

int main() {
  while (true) {
    int N, Q;
    cin >> N >> Q;
    if (N == 0 && Q == 0) {
      return 0;
    }
    MarkedTree tree(N);
    for (int node = 1; node <= N - 1; node++) {
      int parent;
      cin >> parent;
      tree.setParent(node, parent - 1);
    }
    vector<pair<char, int>> inputs;
    inputs.reserve(Q);
    for (int i = 0; i < Q; i++) {
      char q;
      int p;
      cin >> q >> p;
      inputs.emplace_back(q, p);
      if (q == 'M') {
        tree.mark(p - 1);
      }
    }
    int querySum = 0;
    for (int i = (int)inputs.size() - 1; i >= 0; i--) {
      char q = inputs[i].first;
      int node = inputs[i].second;
      if (q == 'Q') {
        querySum += tree.findMarkedParent(node - 1) + 1;
      } else { // Mark
        tree.unMark(node - 1);
      }
    }
    cout << querySum << endl;
  }
  return 0;
}