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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1 << 29)

struct SkewHeap {
  SkewHeap *l, *r;
  int val;
};

SkewHeap *meld(SkewHeap *a, SkewHeap *b) {
  if (a == NULL)
    return b;
  if (b == NULL)
    return a;
  if (a->val < b->val)
    swap(a, b);
  a->r = meld(a->r, b);
  std::swap(a->l, a->r);
  return a;
}
SkewHeap *insert(SkewHeap *a, int v) {
  SkewHeap *b = new SkewHeap;
  b->l = b->r = NULL;
  b->val = v;
  return meld(a, b);
}
int getmax(SkewHeap *a) { return a->val; }
SkewHeap *pop(SkewHeap *a) {
  SkewHeap *res = meld(a->l, a->r);
  delete a;
  return res;
}
void del(SkewHeap *a) {
  if (a->l)
    del(a->l);
  if (a->r)
    del(a->r);
  delete a;
}

int n, q;
vector<int> edge[100000];
int marked[100000];
SkewHeap *bh[100000];
long long ans;

void dfs(int u) {
  for (int i = 0; i < edge[u].size(); i++) {
    dfs(edge[u][i]);
    bh[u] = meld(bh[u], bh[edge[u][i]]);
    bh[edge[u][i]] = NULL;
  }
  while (bh[u] && marked[u] < getmax(bh[u])) {
    ans += u + 1;
    bh[u] = pop(bh[u]);
  }
}

int main() {
  while (cin >> n >> q && n) {
    for (int i = 0; i < n; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++)
      if (bh[i]) {
        del(bh[i]);
        bh[i] = NULL;
      }
    for (int i = 0; i < n; i++)
      marked[i] = INF;
    marked[0] = 0;
    for (int i = 1; i < n; i++) {
      int p;
      cin >> p;
      edge[p - 1].push_back(i);
    }
    for (int i = 0; i < q; i++) {
      char op;
      int v;
      cin >> op >> v;
      v--;
      if (op == 'M') {
        marked[v] = min(marked[v], i + 1);
      } else {
        bh[v] = insert(bh[v], i + 1);
      }
    }
    ans = 0;
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1 << 29)

struct SkewHeap {
  SkewHeap *l, *r;
  int val;
};

SkewHeap *meld(SkewHeap *a, SkewHeap *b) {
  if (a == NULL)
    return b;
  if (b == NULL)
    return a;
  if (a->val < b->val)
    swap(a, b);
  a->r = meld(a->r, b);
  std::swap(a->l, a->r);
  return a;
}
SkewHeap *insert(SkewHeap *a, int v) {
  SkewHeap *b = new SkewHeap;
  b->l = b->r = NULL;
  b->val = v;
  return meld(a, b);
}
int getmax(SkewHeap *a) { return a->val; }
SkewHeap *pop(SkewHeap *a) {
  SkewHeap *res = meld(a->l, a->r);
  delete a;
  return res;
}
void del(SkewHeap *a) {
  if (a->l)
    del(a->l);
  if (a->r)
    del(a->r);
  delete a;
}

int n, q;
vector<int> edge[100000];
int marked[100000];
SkewHeap *bh[100000];
int ans;

void dfs(int u) {
  for (int i = 0; i < edge[u].size(); i++) {
    dfs(edge[u][i]);
    bh[u] = meld(bh[u], bh[edge[u][i]]);
  }
  int t = marked[u];
  while (bh[u] && t < getmax(bh[u])) {
    ans += u + 1;
    bh[u] = pop(bh[u]);
  }
}

int main() {
  while (cin >> n >> q && n) {
    for (int i = 0; i < n; i++)
      edge[i].clear();
    for (int i = 0; i < n; i++)
      if (bh[i]) {
        del(bh[i]);
        bh[i] = NULL;
      }
    for (int i = 0; i < n; i++)
      marked[i] = INF;
    marked[0] = 0;
    for (int i = 1; i < n; i++) {
      int p;
      cin >> p;
      edge[p - 1].push_back(i);
    }
    for (int i = 0; i < q; i++) {
      char op;
      int v;
      cin >> op >> v;
      v--;
      if (op == 'M') {
        marked[v] = min(marked[v], i + 1);
      } else {
        bh[v] = insert(bh[v], i + 1);
      }
    }
    ans = 0;
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}