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

#include <bits/stdc++.h>

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();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

const int nmax = 100010;
const int qmax = 100010;

vi trees[nmax];
int p[nmax];
int last[nmax];

void add_edge(int a, int b) {
  trees[a].pb(b);
  trees[b].pb(a);
}

int uf[nmax];
int find(int i) {
  if (uf[i] == i)
    return i;
  return uf[i] = find(uf[i]);
}

void unite(int p, int c) {
  uf[c] = p;
  return;
}

void dfs(int v, int p) {
  if (last[v] == -1)
    unite(p, v);
  for (auto &e : trees[v]) {
    if (e != p)
      dfs(e, v);
  }
}

char type[qmax];
int num[qmax];

int main(void) {
  int n, q;
  while (cin >> n >> q) {
    if (n == 0)
      break;
    for (int i = 1; i <= n; ++i) {
      trees[i].clear();
      uf[i] = i;
      last[i] = -1;
    }
    for (int i = 2; i <= n; ++i) {
      cin >> p[i];
      add_edge(p[i], i);
    }
    last[1] = -2;
    rep(i, q) {
      cin >> type[i] >> num[i];
      if (type[i] == 'M' && last[num[i]] == -1)
        last[num[i]] = i;
    }
    dfs(1, -1);
    ll ans = 0;
    for (int i = q - 1; i >= 0; --i) {
      if (type[i] == 'Q')
        ans += find(num[i]);
      else {
        if (last[num[i]] == i)
          unite(p[num[i]], num[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0
i = 2
ans = 0
i = 1
ans = 3
i = 0
ans = 3


Fixed submission:

#include <bits/stdc++.h>

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();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

const int nmax = 100010;
const int qmax = 100010;

vi trees[nmax];
int p[nmax];
int last[nmax];

void add_edge(int a, int b) {
  trees[a].pb(b);
  trees[b].pb(a);
}

int uf[nmax];
int find(int i) {
  if (uf[i] == i)
    return i;
  return uf[i] = find(uf[i]);
}

void unite(int p, int c) {
  uf[c] = p;
  return;
}

void dfs(int v, int p) {
  if (last[v] == -1)
    unite(p, v);
  for (auto &e : trees[v]) {
    if (e != p)
      dfs(e, v);
  }
}

char type[qmax];
int num[qmax];

int main(void) {
  int n, q;
  while (cin >> n >> q) {
    if (n == 0)
      break;
    for (int i = 1; i <= n; ++i) {
      trees[i].clear();
      uf[i] = i;
      last[i] = -1;
    }
    for (int i = 2; i <= n; ++i) {
      cin >> p[i];
      add_edge(p[i], i);
    }
    last[1] = -2;
    rep(i, q) {
      cin >> type[i] >> num[i];
      if (type[i] == 'M' && last[num[i]] == -1)
        last[num[i]] = i;
    }
    dfs(1, -1);
    ll ans = 0;
    for (int i = q - 1; i >= 0; --i) {
      dump(i) dump(ans) if (type[i] == 'Q') ans += find(num[i]);
      else {
        if (last[num[i]] == i)
          unite(p[num[i]], num[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}