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

//
//  2170.cpp
//
//
//  Created by Yoshida Satoshi on 2018/10/28.
//

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <utility>
#include <vector>

#define MAX 100010
#define P make_pair
typedef long long ll;

using namespace std;

struct UF {
  vector<int> par;
  vector<int> rank;
  int m[MAX];
  vector<int> c[MAX];
  vector<int> p;
  int n;

  UF(int k) : par(k), rank(k), p(k) {
    n = k;
    for (int i = 0; i < n; i++)
      par[i] = i;
    for (int i = 0; i < n; i++)
      rank[i] = 0;
    for (int i = 0; i < n; i++)
      m[i] = 0;
    m[0] = 1;
  }

  void pushc(int p, int l) { c[p].push_back(l); }

  void mark(int x) { m[x]++; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    return rx == ry;
  }

  void furiwake(int x) {
    for (int i = 0; i < c[x].size(); i++) {
      if (m[c[x][i]] > 0)
        par[c[x][i]] = c[x][i];
      else
        par[c[x][i]] = x;
      furiwake(c[x][i]);
    }
  }

  int score(int x) { return root(x) + 1; }

  void delmark(int x) {
    m[x]--;
    if (m[x] == 0)
      unite(x, p[x]);
  }
};

int main() {
  while (1) {
    int n, q;
    stack<pair<char, int>> qu;
    cin >> n >> q;
    if (n == 0)
      break;
    UF uf(n);
    for (int i = 1; i < n; i++) {
      int p;
      cin >> p;
      uf.pushc(p - 1, i);
      (uf.p)[i] = p - 1;
    }
    for (int i = 0; i < q; i++) {
      char temp;
      int v;
      cin >> temp >> v;
      qu.push(P(temp, v - 1));
      if (temp == 'M')
        uf.mark(v - 1);
    }
    uf.furiwake(0);

    ll ans = 0;
    for (int i = 0; i < q; i++) {
      pair<char, int> a = qu.top();
      qu.pop();
      if (a.first == 'Q') {
        ans += uf.score(a.second);
      } else {
        uf.delmark(a.second);
      }
    }

    cout << ans << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

//
//  2170.cpp
//
//
//  Created by Yoshida Satoshi on 2018/10/28.
//

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <utility>
#include <vector>

#define MAX 100010
#define P make_pair
typedef long long ll;

using namespace std;

struct UF {
  vector<int> par;
  vector<int> rank;
  bool m[MAX];
  vector<int> c[MAX];
  int n;

  UF(int k) : par(k), rank(k) {
    n = k;
    for (int i = 0; i < n; i++)
      par[i] = i;
    for (int i = 0; i < n; i++)
      rank[i] = 0;
    for (int i = 0; i < n; i++)
      m[i] = false;
    m[0] = true;
  }

  void pushc(int p, int l) { c[p].push_back(l); }

  void mark(int x) { m[x] = true; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);

    return rx == ry;
  }

  void furiwake(int x) {
    //  for(int i=0;i<c[x].size();i++)
    {
      // if(m[c[x][i]]) par[c[x][i]]=c[x][i];
      // else           par[c[x][i]]=x;
      //        furiwake(c[x][i]);
    }
  }

  int score(int x) { return root(x) + 1; }

  void delmark(int x) {
    if (!m[x])
      return;
    unite(x + n, x);
  }
};

int main() {
  while (1) {
    int n, q;
    stack<pair<char, int>> qu;
    cin >> n >> q;
    if (n == 0)
      break;
    UF uf(n);
    for (int i = 1; i < n; i++) {
      int p;
      cin >> p;
      uf.pushc(p - 1, i);
    }
    for (int i = 0; i < q; i++) {
      char temp;
      int v;
      cin >> temp >> v;
      qu.push(P(temp, v - 1));
      if (temp == 'M')
        uf.mark(v - 1);
    }
    uf.furiwake(0);

    ll ans = 0;
    for (int i = 0; i < q; i++) {
      pair<char, int> a = qu.top();
      qu.pop();
      if (a.first == 'Q') {
        ans += uf.score(a.second);
      } else {
        uf.delmark(a.second);
      }
    }

    cout << ans << endl;
  }

  return 0;
}