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

#include "bits/stdc++.h"

/*#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>*/
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

// #define int long long
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;

  UnionFind(int N) : parent(N, 0), rank(N, 0) {
    rep(i, 0, N) { parent[i] = i; }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if (x == y) {
      return;
    }

    if (rank[x] < rank[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }
  }

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

vector<int> G[100005];
int anc[100005];

void dfs(int nv, int tanc) {
  if (anc[nv] == tanc || anc[nv] == nv) {
    return;
  }
  anc[nv] = tanc;
  for (auto n : G[nv]) {
    dfs(n, tanc);
  }
}

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

  while (true) {
    int N, Q;
    cin >> N >> Q;
    if (N + Q == 0) {
      break;
    }

    rep(i, 0, N) {
      G[i].clear();
      anc[i] = 0;
    }

    int to;
    rep(i, 0, N - 1) {
      cin >> to;
      G[to - 1].push_back(i + 1);
    }
    char c;
    int v;
    ll ans = 0;
    rep(i, 0, Q) {
      cin >> c >> v;
      if (c == 'M') {
        dfs(v - 1, v - 1);
      } else {
        ans += anc[v - 1] + 1;
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

/*#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>*/
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

// #define int long long
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct UnionFind {
  vector<int> parent;
  vector<int> rank;

  UnionFind(int N) : parent(N, 0), rank(N, 0) {
    rep(i, 0, N) { parent[i] = i; }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if (x == y) {
      return;
    }

    if (rank[x] < rank[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (rank[x] == rank[y]) {
        rank[x]++;
      }
    }
  }

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

vector<int> G[100005];
int anc[100005];

void dfs(int nv, int tanc) {
  anc[nv] = tanc;
  for (auto n : G[nv]) {
    dfs(n, tanc);
  }
}

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

  while (true) {
    int N, Q;
    cin >> N >> Q;
    if (N + Q == 0) {
      break;
    }
    int to;
    rep(i, 0, N - 1) {
      cin >> to;
      G[to - 1].push_back(i + 1);
    }
    char c;
    int v;
    ll ans = 0;
    rep(i, 0, Q) {
      cin >> c >> v;
      if (c == 'M') {
        dfs(v - 1, v - 1);
      } else {
        ans += anc[v - 1] + 1;
      }
    }
    cout << ans << endl;
  }

  return 0;
}