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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Union_Find_Tree {
public:
  vi Par;
  Union_Find_Tree(int n) {
    Par = vi(n);
    for (int i = 0; i < n; i++)
      Par[i] = i;
  }
  int Find_Par(int x) {
    if (Par[x] == x)
      return x;
    return Par[x] = Find_Par(Par[x]);
  }
  bool Unite(int x, int y) {
    x = Find_Par(x);
    y = Find_Par(y);
    if (x == y)
      return 0;
    Par[x] = y;
    return 1;
  }
};

int n, q;
vi a;

int main() {
  while (1) {
    cin >> n >> q;
    if (!n)
      break;
    a = vi(n + 1);
    for (int i = 2; i <= n; i++)
      cin >> a[i];
    vp p(q);
    vi b(n + 1);
    Union_Find_Tree uft(n + 1);
    ll res = 0;
    for (int i = 0; i < q; i++) {
      char c;
      int v;
      cin >> c >> v;
      if (c == 'Q')
        p[i] = {0, v};
      else {
        p[i] = {1, v};
        b[v]++;
      }
    }
    for (int i = 2; i <= n; i++)
      if (!b[i])
        uft.Unite(i, a[i]);
    for (int i = q - 1; i >= 0; i--) {
      bool B = p[i].first;
      int v = p[i].second;
      if (B) {
        b[v]--;
        if (v != 1 && !b[v])
          uft.Unite(v, a[v]);
      } else
        res += uft.Find_Par(v);
    }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

class Union_Find_Tree {
public:
  vi Par;
  Union_Find_Tree(int n) {
    Par = vi(n);
    for (int i = 0; i < n; i++)
      Par[i] = i;
  }
  int Find_Par(int x) {
    if (Par[x] == x)
      return x;
    return Par[x] = Find_Par(Par[x]);
  }
  bool Unite(int x, int y) {
    x = Find_Par(x);
    y = Find_Par(y);
    if (x == y)
      return 0;
    Par[x] = y;
    return 1;
  }
};

int n, q;
vi a;

int main() {
  while (1) {
    cin >> n >> q;
    if (!n)
      break;
    a = vi(n + 1);
    for (int i = 2; i <= n; i++)
      cin >> a[i];
    vp p(n);
    vi b(n + 1);
    Union_Find_Tree uft(n + 1);
    ll res = 0;
    for (int i = 0; i < q; i++) {
      char c;
      int v;
      cin >> c >> v;
      if (c == 'Q')
        p[i] = {0, v};
      else {
        p[i] = {1, v};
        b[v]++;
      }
    }
    for (int i = 2; i <= n; i++)
      if (!b[i])
        uft.Unite(i, a[i]);
    for (int i = q - 1; i >= 0; i--) {
      bool B = p[i].first;
      int v = p[i].second;
      if (B) {
        b[v]--;
        if (!b[v])
          uft.Unite(v, a[v]);
      } else
        res += uft.Find_Par(v);
    }
    cout << res << endl;
  }
}