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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9 + 7

using namespace std;

struct room {
  int depth;
  int doors;
  vector<int> dlist;
  room() : depth(0), doors(0), dlist() {}
};

int main() {
  int n;
  cin >> n;
  REP(counter, n) {
    int num;
    vector<int> data;
    int rs = 0;
    while (cin >> num and num) {
      data.push_back(num);
      if (num > 0)
        rs++;
    }
    vector<room> rooms(rs);
    int currentid = 0;
    int currentdepth = 0;
    vector<int> parents;
    REP(i, data.size()) {
      // cout << data[i] << " : " << currentid << ", " << currentdepth << endl;
      // REP(j, parents.size()) cout << parents[j] << " ";
      // cout << endl;
      if (data[i] > 1) {
        rooms[currentid].doors = data[i];
        rooms[currentid].depth = currentdepth;
        if (parents.size() != 0) {
          int parent = parents[currentdepth - 1];
          // cout << "parent : " << parent << ", ";
          // cout << "dest : " << currentid << endl;
          rooms[parent].dlist.push_back(currentid);
          rooms[currentid].dlist.push_back(parent);
        }
        parents.push_back(currentid);
        currentid++;
        currentdepth++;
      } else if (data[i] == 1) {
        rooms[currentid].doors = data[i];
        rooms[currentid].depth = currentdepth;
        if (parents.size() != 0) {
          int parent = parents[currentdepth - 1];
          // cout << "parent : " << parent << ", ";
          // cout << "dest : " << currentid << endl;
          rooms[parent].dlist.push_back(currentid);
          rooms[currentid].dlist.push_back(parent);
        }
        currentdepth--;
        while (currentdepth >= 0 and
               rooms[parents[currentdepth]].doors ==
                   rooms[parents[currentdepth]].dlist.size()) {
          // cout << "del" << endl;
          parents.pop_back();
          currentdepth--;
        }
        currentdepth++;
        currentid++;
      } else {
        currentdepth--;
        int parent = parents[currentdepth];
        // cout << "parent : " << parent << ", ";
        int dest = parents[currentdepth + data[i]];
        // cout << "dest : " << dest << endl;
        rooms[parent].dlist.push_back(dest);
        rooms[dest].dlist.push_back(parent);
        while (currentdepth >= 0 and
               rooms[parents[currentdepth]].doors ==
                   rooms[parents[currentdepth]].dlist.size()) {
          // cout << "del" << endl;
          parents.pop_back();
          currentdepth--;
        }
        currentdepth++;
      }
    }
    REP(i, rooms.size()) {
      sort(ALL(rooms[i].dlist));
      cout << i + 1;
      REP(j, rooms[i].doors) { cout << " " << rooms[i].dlist[j] + 1; }
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9 + 7

using namespace std;
bool infected[20000];
vector<int> parents;

struct room {
  int depth;
  int doors;
  vector<int> dlist;
  room() : depth(0), doors(0), dlist() {}
};

int main() {
  int n;
  cin >> n;
  REP(counter, n) {
    int num;
    vector<int> data;
    int rs = 0;
    while (cin >> num and num) {
      data.push_back(num);
      if (num > 0)
        rs++;
    }
    vector<room> rooms(rs);
    int currentid = 0;
    int currentdepth = 0;
    vector<int> parents;
    REP(i, data.size()) {
      if (data[i] > 0) {
        rooms[currentid].doors = data[i];
        rooms[currentid].depth = currentdepth;
        if (parents.size() != 0) {
          int parent = parents[currentdepth - 1];
          rooms[parent].dlist.push_back(currentid);
          rooms[currentid].dlist.push_back(parent);
        }
        parents.push_back(currentid);
        currentid++;
        currentdepth++;
      } else {
        currentdepth--;
        int parent = parents[currentdepth];
        int dest = parents[currentdepth + data[i]];
        rooms[parent].dlist.push_back(dest);
        rooms[dest].dlist.push_back(parent);
        while (currentdepth >= 0 and
               rooms[parents[currentdepth]].doors ==
                   rooms[parents[currentdepth]].dlist.size()) {
          parents.pop_back();
          currentdepth--;
        }
        currentdepth++;
      }
    }
    REP(i, rooms.size()) assert(rooms[i].doors == rooms[i].dlist.size());
    REP(i, rooms.size()) {
      sort(ALL(rooms[i].dlist));
      cout << i + 1;
      REP(j, rooms[i].doors) { cout << " " << rooms[i].dlist[j] + 1; }
      cout << endl;
    }
  }
  return 0;
}