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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(n) n.begin(), n.end()
#define each(i, n) for (auto &i : n)
typedef long long ll;

const int INF = 1e9;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

// unionfind??????????????\\??????????????????????????´???
struct UnionFind {
  vector<int> to;
  vector<int> data;
  UnionFind(int size) : to(size, -1), data(size, -1) {}

  //??????x?????????y????¶????????????????(data[root(x | y)] =
  //data[root(x)]???????????????)
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    int rootData = data[root(x)];
    if (x != y) {
      if (to[y] < to[x])
        swap(x, y);
      to[x] += to[y];
      to[y] = x;
    }
    setData(x, rootData);
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return to[x] < 0 ? x : to[x] = root(to[x]); }
  int size(int x) { return -to[root(x)]; }

  int findData(int x) { return data[root(x)]; }
  void setData(int x, int d) { data[root(x)] = d; }
};

signed main() {

  int n, q;
  while (cin >> n >> q && n) {
    struct edge {
      int p, m;
      int count;
      vector<int> t;
    };

    vector<edge> e(n);
    for (int i = 1; i < n; i++) {
      cin >> e[i].p;
      e[i].p--;
      e[i].m = -1;
      e[i].count = 0;
      e[e[i].p].t.push_back(i);
    }
    e[0].m = 0;
    e[0].count = 1;

    vector<int> v(q);
    vector<char> c(q);
    rep(i, q) {
      cin >> c[i] >> v[i];
      v[i]--;
      if (c[i] == 'M') {
        e[v[i]].m = v[i];
        e[v[i]].count++;
      }
    }
    function<void(int, int)> dfs = [&](int a, int b) {
      if (e[a].m == -1) {
        e[a].m = b;
      }
      rep(i, e[a].t.size()) { dfs(e[a].t[i], e[a].m); }
    };
    dfs(0, 0);
    ll ans = 0;
    UnionFind uf(n);
    rep(i, n) { uf.setData(i, i); }

    for (int i = q - 1; i >= 0; i--) {
      int j = v[i];
      if (c[i] == 'M') {
        e[j].count--;
        if (e[j].count == 0) {
          uf.unionSet(e[e[j].p].m, e[j].m);
        }
      } else {
        ans += uf.findData(e[j].m) + 1;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(n) n.begin(), n.end()
#define each(i, n) for (auto &i : n)
typedef long long ll;

const int INF = 1e9;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

// unionfind??????????????\\??????????????????????????´???
struct UnionFind {
  vector<int> to;
  vector<int> data;
  UnionFind(int size) : to(size, -1), data(size, -1) {}

  //??????x?????????y????¶????????????????(data[root(x | y)] =
  //data[root(x)]???????????????)
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    int rootData = data[root(x)];
    if (x != y) {
      if (to[y] < to[x])
        swap(x, y);
      to[x] += to[y];
      to[y] = x;
    }
    setData(x, rootData);
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return to[x] < 0 ? x : to[x] = root(to[x]); }
  int size(int x) { return -to[root(x)]; }

  int findData(int x) { return data[root(x)]; }
  void setData(int x, int d) { data[root(x)] = d; }
};

signed main() {

  int n, q;
  while (cin >> n >> q && n) {
    struct edge {
      int p, m;
      int count;
      vector<int> t;
    };

    vector<edge> e(n);
    for (int i = 1; i < n; i++) {
      cin >> e[i].p;
      e[i].p--;
      e[i].m = -1;
      e[i].count = 0;
      e[e[i].p].t.push_back(i);
    }
    e[0].m = 0;
    e[0].count = 1;

    vector<int> v(q);
    vector<char> c(q);
    rep(i, q) {
      cin >> c[i] >> v[i];
      if (c[i] == 'M') {
        e[v[i]].m = --v[i];
        e[v[i]].count++;
      }
    }
    function<void(int, int)> dfs = [&](int a, int b) {
      if (e[a].m == -1) {
        e[a].m = b;
      }
      rep(i, e[a].t.size()) { dfs(e[a].t[i], e[a].m); }
    };
    dfs(0, 0);
    ll ans = 0;
    UnionFind uf(n);

    for (int i = q - 1; i >= 0; i--) {
      int j = v[i];
      if (c[i] == 'M') {
        e[j].count--;
        if (e[j].count == 0) {
          uf.unionSet(e[e[j].p].m, e[j].m);
        }
      } else {
        ans += uf.findData(e[j].m) + 1;
      }
    }

    cout << ans << endl;
  }

  return 0;
}