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
n = 3
I,J = 1 3
I,J = 2 0
I,J = 0 0
n = 4
I,J = 4 1
I,J = 3 1
I,J = 1 2
I,J = 0 0
n = 5
I,J = 2 1
I,J = 3 1
I,J = 4 1
I,J = 3 2
I,J = 1 1
I,J = 0 0
n = 0


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#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 EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(), n.end()
#define pb push_back
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define INF (1 << 27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, P> PP;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

vector<short> st[200];
map<int, P> Index; // value - map no pos,map no naka no pos(0 start)
int n;

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

int main() {

  while (true) {
    cin >> n;
    cerr << "n = " << n << endl;
    if (n == 0)
      break;
    Index.clear();
    rep(i, n + 1) st[i].clear();

    for (int i = 1; i <= n; i++) {
      Index[i] = P(i, 0);
      st[i].pb(i); // ue kazoeru you
    }

    int I, J;
    while (true) {
      cin >> I >> J;
      cerr << "I,J = " << I << " " << J << endl;
      if (I + J == 0)
        break;
      if (I == J)
        continue;
      // cout << "I = " << I << ", J = " << J << endl;
      if (Index[I].F == Index[J].F) {
        if (Index[I].S >= Index[J].S) {
          // cout << "miss - 1" << endl;
          continue;
        }
      }

      if (I == 0)
        continue;
      // if(J == 0){
      // int posss = posGet();
      // st[Index[I].pos].
      // st[posss].pb(I);
      // Index[I] = P(posss,0);
      //}

      // if(Index[I].S == 0 && Index[J].S == 0 ){
      // cout << "miss - 2" << endl;
      // continue; // yuka kara yuka
      // }

      // cout << "Index[" << I << "].S = "<< Index[I].S << ", st["<< Index[I].F
      // << "].size()-1 = " << st[Index[I].F].size()-1 << endl;
      if (Index[I].S != st[Index[I].F].size() - 1) {
        while (Index[I].S != st[Index[I].F].size() - 1) {
          int val = st[Index[I].F][st[Index[I].F].size() - 1];
          // cout << "take off val = " << val << endl;
          st[Index[I].F].pop_back();
          int poss = posGet();
          st[poss].pb(val);
          Index[val] = P(poss, 0);
        }
      }
      int val = st[Index[I].F][st[Index[I].F].size() - 1];
      st[Index[I].F].pop_back();
      st[Index[J].F].pb(val);
      Index[val] = P(Index[J].F, st[Index[J].F].size() - 1);
    }
    vector<int> ans;
    int total = 0;
    for (int i = 0; i <= n; i++) {
      if (st[i].size() == 0)
        continue;
      ans.pb(st[i].size());
      total += st[i].size();
    }

    if (total <= 1) {
      // cout << "a pile." << endl;
      // cout << "end" << endl;
      // continue;
    }

    sort(all(ans));
    rep(i, ans.size()) { cout << ans[i] << endl; }
    cout << "end" << endl;
  }

  return 0;
}