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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
  int m;
  cin >> m;
  cin.ignore();
  while (m--) {
    int tmp;
    vector<int> v;
    while (cin >> tmp && tmp != 0)
      v.push_back(tmp);
    int n = v.size();
    vector<int> to[200];
    int cur = 0;
    int mem[200] = {};
    int rest[200] = {};
    int level = 0;
    REP(i, n) {
      while (level >= 0 && rest[level] == 0)
        level--;
      // printf("v[%d] = %d , lev = %d\\n", i, v[i], lev);
      if (v[i] > 0) {
        if (level >= 0) {
          // printf("%d <-> %d\\n", mem[lev - 1], cur);
          to[mem[level]].push_back(cur);
          to[cur].push_back(mem[level]);
        }
        mem[level + 1] = cur++;
        rest[level]--;
        rest[level + 1] = v[i] - 1;
        level++;
      } else if (v[i] < 0) {
        // printf("%d <-> %d\\n", mem[lev], mem[lev2]);
        to[mem[level]].push_back(mem[level + v[i]]);
        to[mem[level + v[i]]].push_back(mem[level]);
        rest[level]--;
        rest[level + v[i]]--;
      }
    }
    REP(i, cur) {
      printf("%d ", i + 1);
      sort(to[i].begin(), to[i].end());
      REP(j, to[i].size()) {
        printf("%d", to[i][j] + 1);
        if (j == to[i].size() - 1)
          cout << endl;
        else
          cout << " ";
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main() {
  int m;
  cin >> m;
  cin.ignore();
  while (m--) {
    int tmp;
    vector<int> v;
    while (cin >> tmp && tmp != 0)
      v.push_back(tmp);
    int n = v.size();
    vector<int> to[200];
    int size[200];
    int cur = 0;
    stack<int> stk;
    stk.push(0);
    int mem[200] = {};
    REP(i, n) {
      int lev = stk.top();
      stk.pop();
      // printf("v[%d] = %d , lev = %d\\n", i, v[i], lev);
      if (v[i] > 0) {
        if (lev > 0) {
          // printf("%d <-> %d\\n", mem[lev - 1], cur);
          to[mem[lev - 1]].push_back(cur);
          to[cur].push_back(mem[lev - 1]);
        }
        size[cur] = v[i];
        mem[lev] = cur++;
        REP(j, v[i] - 1) stk.push(lev + 1);
      } else if (v[i] < 0) {
        lev--;
        int lev2 = lev + v[i];
        // printf("%d <-> %d\\n", mem[lev], mem[lev2]);
        to[mem[lev]].push_back(mem[lev2]);
        to[mem[lev2]].push_back(mem[lev]);
      }
    }
    REP(i, cur) {
      printf("%d ", i + 1);
      assert(to[i].size() == size[i]);
      sort(to[i].begin(), to[i].end());
      REP(j, to[i].size()) {
        printf("%d", to[i][j] + 1);
        if (j == to[i].size() - 1)
          cout << endl;
        else
          cout << " ";
      }
    }
  }
  return 0;
}