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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

// ios::sync_with_stdio(false);

using namespace std;

const int INF = (1 << 30) - 1;

const int MAX_N = 100000;

int _par[MAX_N];

int ancestor(vector<int> &parents, vector<int> &marked, int u) {
  int &p = parents[u];
  if (marked[p])
    return _par[u] = p;
  return _par[u] = ancestor(parents, marked, p);
}

void init(vector<int> &parents, vector<int> &marked) {
  int n = marked.size();
  for (int i = 0; i < n; i++)
    _par[i] = i;

  for (int i = 1; i < n; i++) {
    if (marked[i] == 0 && _par[i] == i)
      ancestor(parents, marked, i);
  }
}

int root(int u) {
  if (_par[u] == u)
    return u;
  return _par[u] = root(_par[u]);
}

int main() {
  int N, Q;
  while (cin >> N >> Q, N | Q) {
    vector<int> parents(N);
    parents[0] = 0;
    for (int i = 1; i < N; i++) {
      cin >> parents[i];
      parents[i]--;
    }

    vector<pair<string, int>> operations(Q);
    for (int i = 0; i < Q; i++) {
      cin >> operations[i].first >> operations[i].second;
      operations[i].second--;
    }

    vector<int> marked(N);
    marked[0] = 1;
    for (auto &o : operations) {
      if (o.first == "M")
        marked[o.second]++;
    }

    init(parents, marked);

    long long int ans = 0;
    for (int i = Q - 1; i >= 0; i--) {
      auto &o = operations[i];
      int &u = o.second;
      if (o.first == "M") {
        if (--marked[u] == 0)
          _par[u] = parents[_par[u]];
      } else if (o.first == "Q") {
        ans += root(u) + 1;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

// ios::sync_with_stdio(false);

using namespace std;

const int INF = (1 << 30) - 1;

const int MAX_N = 100000;

int _par[MAX_N];

int ancestor(vector<int> &parents, vector<bool> &marked, int u) {
  int &p = parents[u];
  if (marked[p])
    return _par[u] = p;
  return _par[u] = ancestor(parents, marked, p);
}

void init(vector<int> &parents, vector<bool> &marked) {
  int n = marked.size();
  for (int i = 0; i < n; i++)
    _par[i] = i;

  for (int i = 1; i < n; i++) {
    if (!marked[i] && _par[i] == i)
      ancestor(parents, marked, i);
  }
}

int root(int u) {
  if (_par[u] == u)
    return u;
  return _par[u] = root(_par[u]);
}

int main() {
  int N, Q;
  while (cin >> N >> Q, N | Q) {
    vector<int> parents(N);
    parents[0] = -1;
    for (int i = 1; i < N; i++) {
      cin >> parents[i];
      parents[i]--;
    }

    vector<pair<string, int>> operations(Q);
    for (int i = 0; i < Q; i++) {
      cin >> operations[i].first >> operations[i].second;
      operations[i].second--;
    }

    vector<bool> marked(N);
    marked[0] = true;
    for (auto &o : operations) {
      if (o.first == "M")
        marked[o.second] = true;
    }

    init(parents, marked);

    int ans = 0;
    for (int i = N - 1; i >= 0; i--) {
      auto &o = operations[i];
      int &u = o.second;
      if (o.first == "M") {
        marked[u] = false;
        _par[u] = parents[_par[u]];
      } else if (o.first == "Q") {
        ans += root(u) + 1;
      }
    }
    cout << ans << endl;
  }

  return 0;
}