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

#include <algorithm>
#include <iostream>
#include <vector>
#define llint long long

using namespace std;

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

  UnionFind() {}
  UnionFind(int size) {
    this->size = size;
    parent.resize(size + 1);
    init();
  }
  void init() {
    for (int i = 0; i <= size; i++)
      parent[i] = i;
  }
  int root(int i) {
    if (parent[i] == i)
      return i;
    return parent[i] = root(parent[i]);
  }
  bool same(int i, int j) { return root(i) == root(j); }
  void unite(int i, int j) {
    int root_i = root(i), root_j = root(j);
    if (root_i == root_j)
      return;
    parent[root_i] = root_j;
  }
};

int n, q;
int p[100005];
vector<int> G[100005];
char c[100005];
int v[100005];
bool mark[100005];
UnionFind uf(100005);

void dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (!mark[G[v][i]])
      uf.unite(G[v][i], v);
    dfs(G[v][i]);
  }
}

int main(void) {
  while (1) {
    cin >> n >> q;
    if (n == 0 && q == 0)
      break;

    for (int i = 1; i <= n; i++)
      G[i].clear();
    for (int i = 2; i <= n; i++) {
      cin >> p[i];
      G[p[i]].push_back(i);
    }
    for (int i = 0; i < q; i++) {
      cin >> c[i] >> v[i];
    }

    for (int i = 1; i <= n; i++)
      mark[i] = false;
    mark[1] = true;

    for (int i = 0; i < q; i++) {
      if (c[i] == 'M') {
        if (mark[v[i]])
          c[i] = 'X';
        else
          mark[v[i]] = true;
      }
    }
    uf.init();
    dfs(1);

    reverse(c, c + q);
    reverse(v, v + q);

    llint ans = 0;
    for (int i = 0; i < q; i++) {
      if (c[i] == 'Q')
        ans += uf.root(v[i]);
      else if (c[i] == 'M' && v[i] != 1)
        uf.unite(v[i], p[v[i]]);
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define llint long long

using namespace std;

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

  UnionFind() {}
  UnionFind(int size) {
    this->size = size;
    parent.resize(size + 1);
    init();
  }
  void init() {
    for (int i = 0; i <= size; i++)
      parent[i] = i;
  }
  int root(int i) {
    if (parent[i] == i)
      return i;
    return parent[i] = root(parent[i]);
  }
  bool same(int i, int j) { return root(i) == root(j); }
  void unite(int i, int j) {
    int root_i = root(i), root_j = root(j);
    if (root_i == root_j)
      return;
    parent[root_i] = root_j;
  }
};

int n, q;
int p[100005];
vector<int> G[100005];
char c[100005];
int v[100005];
bool mark[100005];
UnionFind uf(100005);

void dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (!mark[G[v][i]])
      uf.unite(G[v][i], v);
    dfs(G[v][i]);
  }
}

int main(void) {
  while (1) {
    cin >> n >> q;
    if (n == 0 && q == 0)
      break;

    for (int i = 2; i <= n; i++) {
      cin >> p[i];
      G[p[i]].push_back(i);
    }
    for (int i = 0; i < q; i++) {
      cin >> c[i] >> v[i];
    }

    for (int i = 1; i <= n; i++)
      mark[i] = false;
    mark[1] = true;

    for (int i = 0; i < q; i++) {
      if (c[i] == 'M')
        mark[v[i]] = true;
    }
    uf.init();
    dfs(1);

    reverse(c, c + q);
    reverse(v, v + q);

    llint ans = 0;
    for (int i = 0; i < q; i++) {
      if (c[i] == 'Q')
        ans += uf.root(v[i]);
      else
        uf.unite(v[i], p[v[i]]);
    }
    cout << ans << endl;
  }

  return 0;
}