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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1 << 28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  while (n--) {
    vi a, c, d;
    vector<vi> v(100);
    int in;
    while (cin >> in, in)
      a.pb(in);
    int cur = 0, top = 1;
    c.pb(0);
    d.pb(a[0]);
    FOR(i, 1, a.size()) {
      if (a[i] > 0) {
        c.pb(c[cur] + 1);
        d.pb(a[i]);
        v[top].pb(cur);
        v[cur].pb(top);
        d[top]--;
        d[cur]--;
        cur = top++;
      } else {
        int to;
        for (int j = cur; j >= 0; j--) {
          if (c[j] - c[cur] == a[i] && d[j] > 0) {
            to = j;
            break;
          }
        }
        v[to].pb(cur);
        v[cur].pb(to);
        d[to]--;
        d[cur]--;
        if (i == a.size() - 1)
          break;
      }
      while (!d[cur])
        cur--;
    }
    REP(i, top) {
      cout << i + 1;
      sort(v[i].begin(), v[i].end());
      REP(j, v[i].size())
      cout << ' ' << v[i][j] + 1;
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int INF = 1 << 28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

void add_Node(int a, int b, vector<vi> &node, vi &edge) {
  node[a].pb(b);
  node[b].pb(a);
  edge[a]--;
  edge[b]--;
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  while (n--) {
    vi edge, cur;
    vector<vi> node(1000);
    int in, cnt = -1, times = -1, pre;
    while (cin >> in, in) {
      if (in > 0) {
        cnt++;
        times++;
        // cout << times+1 << ' ' << cnt << endl;
        edge.pb(in);
        if (times)
          add_Node(times, pre, node, edge);
        cur.pb(cnt);
        pre = times;
      } else if (in < 0) {
        // REP(i, edge.size())
        // cout << edge[i] << ' ';

        int pnt;
        for (pnt = times; pnt >= 0; pnt--) {
          // cout << cur[pnt] << ' ' << cnt << ' ' << edge[pnt] << endl;
          if (cur[pnt] - cnt == in && edge[pnt] > 0)
            break;
        }
        // cout << times+1 << ' ' << pnt+1 << endl;
        add_Node(pre, pnt, node, edge);
        while (!edge[pre])
          pre--;
        cnt = cur[pre];
        // cout << pre+1 << endl;
      }
    }
    REP(i, node.size()) {
      if (!node[i].size())
        continue;
      cout << i + 1;
      sort(node[i].begin(), node[i].end());
      REP(j, node[i].size())
      cout << ' ' << node[i][j] + 1;
      cout << endl;
    }
  }
  return 0;
}