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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

VI sz, xs, dist, prv;
VVI edge;
int M;
int dfs(int &i, int cur) {
  if (i == SZ(xs))
    return cur;
  if (xs[i] < 0) {
    cur = prv[cur];
    int d = -xs[i], p = prv[cur];
    while (true) {
      if (dist[cur] - dist[p] == d)
        break;
      p = prv[p];
    }

    --sz[cur];
    --sz[p];
    edge[cur].PB(p);
    edge[p].PB(cur);
    ++i;
    return -1;
  }

  int nxt = cur + 1;
  ++i;
  while (sz[cur] > 0) {
    --sz[cur];
    --sz[nxt];
    prv[nxt] = cur;
    dist[nxt] = dist[cur] + 1;
    int nxt_ = dfs(i, nxt);
    if (nxt_ >= 0) {
      edge[cur].PB(nxt);
      edge[nxt].PB(cur);
      nxt = nxt_;
    } else {
      ++sz[cur];
      ++sz[nxt];
      dist[nxt] = 0;
    }
  }

  return nxt;
}

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

  int N;
  cin >> N;
  while (N--) {
    xs.clear();
    sz.clear();
    M = 0;
    while (true) {
      int x;
      cin >> x;
      if (x == 0)
        break;
      xs.PB(x);
      if (x > 0) {
        sz.PB(x);
        ++M;
      }
    }
    prv.assign(110, -1);
    dist.assign(110, 0);
    edge.assign(110, VI());
    int it = 0, nxt = 0;
    dfs(it, 0);

    for (int i = 0; i < M; ++i) {
      cout << i + 1;
      SORT(edge[i]);
      for (int j = 0; j < SZ(edge[i]); ++j)
        cout << " " << edge[i][j] + 1;
      cout << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

VI sz, xs, dist, prv;
VVI edge;
int M;
int dfs(int &i, int cur) {
  if (i == SZ(xs))
    return cur;
  if (sz[cur] == 0)
    return cur + 1;
  if (xs[i] < 0) {
    ++sz[cur];
    ++sz[prv[cur]];
    int res = cur;
    cur = prv[cur];
    int d = -xs[i], p = prv[cur];
    while (true) {
      if (dist[cur] - dist[p] == d)
        break;
      p = prv[p];
      /*
      if(p == -1){
            cout << "HALT with " << i << ", " << cur << endl;
            for(int i=0;i<M;++i) cout << prv[i] << " ";
            cout << endl;
            break;
      }
      */
    }

    --sz[cur];
    --sz[p];
    edge[cur].PB(p);
    edge[p].PB(cur);
    ++i;
    return res;
  }
  // cout << i << " " << cur << " " << sz[cur] << endl;
  int nxt = cur + 1;
  ++i;
  while (sz[cur] > 0) {
    --sz[cur];
    --sz[nxt];
    prv[nxt] = cur;
    dist[nxt] = dist[cur] + 1;
    int nxt_ = dfs(i, nxt);
    if (nxt_ != nxt) {
      edge[cur].PB(nxt);
      edge[nxt].PB(cur);
      nxt = nxt_;
    }
  }

  return nxt;
}

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

  int N;
  cin >> N;
  while (N--) {
    xs.clear();
    sz.clear();
    M = 0;
    while (true) {
      int x;
      cin >> x;
      if (x == 0)
        break;
      xs.PB(x);
      if (x > 0) {
        sz.PB(x);
        ++M;
      }
    }
    prv.assign(101, -1);
    dist.assign(101, 0);
    edge.assign(101, VI());
    int it = 0;
    dfs(it, 0);

    for (int i = 0; i < M; ++i) {
      cout << i + 1;
      SORT(edge[i]);
      for (int j = 0; j < SZ(edge[i]); ++j)
        cout << " " << edge[i][j] + 1;
      cout << endl;
    }
  }

  return 0;
}