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

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

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<ll, ll> PI;
#define EPS (1e-10L)
#define rep(i, n) for (ll i = 0; i < (int)(n); ++i)
#define REP(i, m, n) for (ll i = m; i <= (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
ll dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

vector<int> in;

vector<int> G[1000];
int pos = 0;
int path[1000];
int ed[1000];
int newn;

void dfs(int cv, int depth) {
  // cout << cv << ' ' << depth << endl;

  path[depth] = cv;
  ed[cv] = in[pos++];
  bool ok = false;
  if (SZ(G[cv]) == ed[cv])
    return;
  while (pos < SZ(in)) {
    int t = in[pos];
    // cout << t << endl;
    if (t < 0) {
      assert(depth + t >= 0);
      int nv = path[depth + t];
      G[cv].pb(nv);
      G[nv].pb(cv);
      ++pos;
    } else {
      ++newn;
      G[cv].pb(newn);
      G[newn].pb(cv);
      dfs(newn, depth + 1);
      assert(SZ(G[newn]) == ed[newn]);
    }
    if (SZ(G[cv]) == ed[cv]) {
      ok = true;
      break;
    }
  }
  assert(ok);
}

vector<int> killedge(vector<int> in) {
  rep(i, SZ(in)) if (in[i] < 0) {
    --in[i + in[i]];
    --in[i - 1];
    in.erase(in.begin() + i);
    break;
  }
  return in;
}

void solve() {
  in.clear();
  int t;
  while (cin >> t, t)
    in.pb(t);

  while (true) {
    int no = 0;
    int degsum = 0;
    newn = 0;
    FOR(it, in) {
      int t = *it;
      no += t > 0;
      degsum += (t > 0) * t;
    }

    rep(i, no) G[i].clear();
    pos = 0;
    try {
      dfs(0, 0);
    } catch (...) {
      in = killedge(in);
      continue;
    }
    rep(i, no) sort(ALL(G[i]));

    rep(i, no) {
      cout << i + 1;
      FOR(it, G[i]) cout << ' ' << *it + 1;
      cout << endl;
    }
    break;
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n) solve();
}

Error: 0

Fixed submission:

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

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<ll, ll> PI;
#define EPS (1e-10L)
#define rep(i, n) for (ll i = 0; i < (int)(n); ++i)
#define REP(i, m, n) for (ll i = m; i <= (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
ll dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

vector<int> in;

vector<int> G[1000];
int pos = 0;
int path[1000];
int ed[1000];
int newn;

void dfs(int cv, int depth) {
  // cout << cv << ' ' << depth << endl;
  path[depth] = cv;
  ed[cv] = in[pos++];
  bool ok = false;
  while (pos < SZ(in)) {
    int t = in[pos];
    // cout << t << endl;
    if (t < 0) {
      assert(depth + t >= 0);
      int nv = path[depth + t];
      G[cv].pb(nv);
      G[nv].pb(cv);
      ++pos;
    } else {
      ++newn;
      G[cv].pb(newn);
      G[newn].pb(cv);
      dfs(newn, depth + 1);
    }
    if (SZ(G[cv]) == ed[cv]) {
      ok = true;
      break;
    }
  }
  if (!ok) {
    FOR(it, in) cerr << *it << ' ';
  }
  assert(ok);
}

void solve() {
  newn = 0;
  in.clear();
  int t;
  int no = 0;
  while (cin >> t, t) {
    no += t > 0;
    in.pb(t);
  }

  rep(i, no) G[i].clear();
  pos = 0;
  dfs(0, 0);
  rep(i, no) sort(ALL(G[i]));

  rep(i, no) {
    cout << i + 1;
    FOR(it, G[i]) cout << ' ' << *it + 1;
    cout << endl;
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n) solve();
}