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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
int n;
vector<int> que[200];
map<int, P> dex;

int getEmptyArea() {
  for (int i = 1; i <= n; i++) {
    if (que[i].size() == 0)
      return i;
  }
  assert(false);
}

int main() {

  while (true) {
    cin >> n;

    if (n == 0)
      break;

    dex.clear();
    que[0].clear();
    for (int i = 1; i <= n; i++) {
      que[i].clear();
      que[i].pb(i);
      dex[i] = P(i, 0); // P(場所,番地)
    }

    while (true) { //
      int I, J;
      cin >> I >> J;

      if (I + J == 0)
        break;
      if (que[dex[I].F].size() == 0)
        continue; // とるものがない
      if (dex[I].S == 0 && J == 0)
        continue;
      if (dex[I].F == dex[J].F && dex[I].S >= dex[J].S)
        continue; // 置く場所が下にある

      int pos = que[dex[I].F].size() - 1;

      while (que[dex[I].F][pos] != I) {
        int emp = getEmptyArea(), val = que[dex[I].F][pos];
        que[dex[I].F].pop_back();
        que[emp].pb(val);
        dex[val] = P(emp, 0);
        pos--;
        assert(pos >= -1);
      }

      assert(I == que[dex[I].F][pos]);
      que[dex[I].F].pop_back();
      int emp = getEmptyArea();
      J == 0 ? que[emp].pb(I) : que[dex[J].F].pb(I);
      J == 0 ? dex[I] = P(emp, 0) : dex[I] = P(dex[J].F, que[J].size() - 1);

    } //
    vector<int> stac;
    for (int i = 1; i <= n; i++) {
      if (que[i].size() == 0)
        continue;
      stac.pb(que[i].size());
    }
    sort(all(stac));
    rep(i, stac.size()) { cout << stac[i] << endl; }
    cout << "end" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define pf push_front
#define LIM 100000
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;
int n;
vector<int> que[200];
map<int, P> dex;

int getEmptyArea() {
  for (int i = 1; i <= n; i++) {
    if (que[i].size() == 0)
      return i;
  }
  assert(false);
}

int main() {

  while (true) {
    cin >> n;
    if (n == 0)
      break;

    dex.clear();
    que[0].clear();
    for (int i = 1; i <= n; i++) {
      que[i].clear();
      que[i].pb(i);
      dex[i] = P(i, 0); // P(場所,番地)
    }

    while (true) { //
      int I, J;
      cin >> I >> J;
      if (I + J == 0)
        break;
      if (que[dex[I].F].size() == 0)
        continue; // とるものがない
      if (dex[I].S == 0 && J == 0)
        continue;
      if (dex[I].F == dex[J].F && dex[I].S >= dex[J].S)
        continue; // 置く場所が下にある

      int pos = que[dex[I].F].size() - 1;

      while (que[dex[I].F][pos] != I) {
        int emp = getEmptyArea(), val = que[dex[I].F][pos];
        que[dex[I].F].pop_back();
        que[emp].pb(val);
        dex[val] = P(emp, 0);
        pos--;
        assert(pos >= -1);
      }

      assert(I == que[dex[I].F][pos]);
      que[dex[I].F].pop_back();
      int emp = getEmptyArea();
      J == 0 ? que[emp].pb(I) : que[dex[J].F].pb(I);
      J == 0 ? dex[I] = P(emp, 0) : dex[I] = P(J, que[J].size() - 1);

    } //
    vector<int> stac;
    for (int i = 1; i <= n; i++) {
      if (que[i].size() == 0)
        continue;
      stac.pb(que[i].size());
    }
    sort(all(stac));
    rep(i, stac.size()) { cout << stac[i] << endl; }
    cout << "end" << endl;
  }
  return 0;
}