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

#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> graph;
typedef pair<char, int> query;

vector<int> mark_parent;
vector<int> marked;
graph g;

void walk_graph(int v, int mark_p) {
  mark_parent[v] = mark_p;

  if (marked[v]) {
    mark_p = v;
  }

  for (int &nv : g[v]) {
    walk_graph(nv, mark_p);
  }
}

int find_mark_parent(int v) {
  if (marked[v]) {
    // cout << endl;
    return v;
  }
  // cout << v << "->";
  return mark_parent[v] = find_mark_parent(mark_parent[v]);
}

long long solve(int N, int Q, vector<int> &parent, vector<query> &queries) {
  mark_parent = vector<int>(N, 0);
  marked = vector<int>(N, 0);

  g = graph(N, vector<int>());

  for (int j = 1; j < N; ++j) {
    g[parent[j]].push_back(j);
  }

  marked[0] = 1;
  for (query &q : queries) {
    if (q.first == 'M') {
      ++marked[q.second];
    }
  }

  walk_graph(0, 0);

  long long ret = 0;

  for (int j = Q - 1; j >= 0; --j) {
    char ch = queries[j].first;
    int v = queries[j].second;

    if (ch == 'Q') {
      int ind = find_mark_parent(v) + 1;
      ret += ind;
    }

    if (ch == 'M') {
      --marked[v];
    }
  }

  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N, Q;
  vector<int> parent;
  vector<query> queries;

  while (true) {
    cin >> N >> Q;
    if (N == 0 && Q == 0) {
      break;
    }

    parent.resize(N);
    parent[0] = 0;
    for (int j = 1; j < N; ++j) {
      cin >> parent[j];
      --parent[j];
    }

    queries.resize(Q);
    for (query &q : queries) {
      cin >> q.first >> q.second;
      --q.second;
    }

    cout << solve(N, Q, parent, queries) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> graph;
typedef pair<char, int> query;

void walk_graph(int v, int mark_p, vector<int> &mark_parent,
                vector<int> &marked, graph &g) {

  mark_parent[v] = mark_p;

  if (marked[v]) {
    mark_p = v;
  }

  for (int &nv : g[v]) {
    walk_graph(nv, mark_p, mark_parent, marked, g);
  }
}

int find_mark_parent(int v, vector<int> &mark_parent, vector<int> &marked) {
  if (marked[v])
    return v;
  mark_parent[v] = find_mark_parent(mark_parent[v], mark_parent, marked);
  return mark_parent[v];
}

int solve(int N, int Q, vector<int> &parent, vector<query> &queries) {
  vector<int> mark_parent(N, 0);
  vector<int> marked(N, 0);

  graph g = graph(N, vector<int>());

  for (int j = 1; j < N; ++j) {
    g[parent[j]].push_back(j);
  }

  marked[0] = 1;
  for (query &q : queries) {
    if (q.first == 'M') {
      marked[q.second] = 1;
    }
  }

  walk_graph(0, -1, mark_parent, marked, g);

  int ret = 0;

  for (int j = Q - 1; j >= 0; --j) {
    char ch = queries[j].first;
    int v = queries[j].second;

    if (ch == 'Q') {
      ret += find_mark_parent(v, mark_parent, marked) + 1;
    }

    if (ch == 'M') {
      marked[v] = 0;
    }
  }

  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N, Q;
  vector<int> parent;
  vector<query> queries;

  while (true) {
    cin >> N >> Q;
    if (N == 0 && Q == 0) {
      break;
    }

    parent.resize(N);
    parent[0] = 0;
    for (int j = 1; j < N; ++j) {
      cin >> parent[j];
      --parent[j];
    }

    queries.resize(Q);
    for (query &q : queries) {
      cin >> q.first >> q.second;
      --q.second;
    }

    cout << solve(N, Q, parent, queries) << endl;
  }
  return 0;
}