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

#include <iostream>
#include <stack>
#include <unordered_set>
using namespace std;

int main(int argc, const char *argv[]) {
  int N, M;
  cin >> N >> M;

  unordered_set<stack<int> *> stcGrp;

  stack<int> **grps = new stack<int> *[N];
  for (int i = 0; i < N; ++i) {
    grps[i] = NULL;
  }
  int max = 1;
  for (int i = 0; i < M; ++i) {
    int a1, b1;
    cin >> a1 >> b1;
    --a1;
    --b1;
    if (grps[a1] == NULL) {
      if (grps[b1] == NULL) {
        stack<int> *nGrp = new stack<int>();
        stcGrp.insert(nGrp);
        nGrp->push(a1);
        nGrp->push(b1);
        grps[a1] = nGrp;
        grps[b1] = nGrp;
        if (max < nGrp->size()) {
          max = (int)nGrp->size();
        }
      } else {
        grps[a1] = grps[b1];
        grps[b1]->push(a1);
        if (max < grps[b1]->size()) {
          max = (int)grps[b1]->size();
        }
      }
    } else {
      if (grps[b1] == NULL) {
        grps[b1] = grps[a1];
        grps[a1]->push(b1);
        if (max < grps[a1]->size()) {
          max = (int)grps[a1]->size();
        }
      } else if (grps[b1] != grps[a1]) {
        stack<int> *delGrp, *tGrp;
        if (grps[b1]->size() < grps[a1]->size()) {
          delGrp = grps[b1];
          tGrp = grps[a1];
        } else {
          delGrp = grps[a1];
          tGrp = grps[b1];
        }
        while (!delGrp->empty()) {
          int idx = delGrp->top();
          delGrp->pop();
          tGrp->push(idx);
          grps[idx] = tGrp;
        }
        stcGrp.erase(delGrp);
        delete delGrp;
        if (max < tGrp->size()) {
          max = (int)tGrp->size();
        }
      }
    }
  }
  cout << max << endl;
  for (unordered_set<stack<int> *>::iterator itr = stcGrp.begin(),
                                             end = stcGrp.end();
       itr != end; ++itr) {
    delete *itr;
  }
  delete[] grps;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <unordered_set>
using namespace std;

int main(int argc, const char *argv[]) {
  int N, M;
  cin >> N >> M;

  unordered_set<stack<int> *> stcGrp;

  stack<int> **grps = new stack<int> *[N];
  for (int i = 0; i < N; ++i) {
    grps[i] = NULL;
  }
  int max = 1;
  for (int i = 0; i < M; ++i) {
    int a1, b1;
    cin >> a1 >> b1;
    --a1;
    --b1;
    if (grps[a1] == NULL) {
      if (grps[b1] == NULL) {
        stack<int> *nGrp = new stack<int>();
        stcGrp.insert(nGrp);
        nGrp->push(a1);
        nGrp->push(b1);
        grps[a1] = nGrp;
        grps[b1] = nGrp;
        if (max < nGrp->size()) {
          max = (int)nGrp->size();
        }
      } else {
        grps[a1] = grps[b1];
        grps[b1]->push(a1);
        if (max < grps[b1]->size()) {
          max = (int)grps[b1]->size();
        }
      }
    } else {
      if (grps[b1] == NULL) {
        grps[b1] = grps[a1];
        grps[a1]->push(b1);
        if (max < grps[a1]->size()) {
          max = (int)grps[a1]->size();
        }
      } else if (grps[b1] != grps[a1]) {
        stack<int> *delGrp = grps[a1];
        while (!delGrp->empty()) {
          int idx = delGrp->top();
          delGrp->pop();
          grps[b1]->push(idx);
          grps[i] = grps[b1];
        }
        stcGrp.erase(delGrp);
        delete delGrp;
      }
    }
  }
  cout << max << endl;
  for (unordered_set<stack<int> *>::iterator itr = stcGrp.begin(),
                                             end = stcGrp.end();
       itr != end; ++itr) {
    delete *itr;
  }
  delete[] grps;
  return 0;
}