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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

pair<int, int> find(int n, vvi const &v) {
  rep(i, v.size()) rep(j, v[i].size()) {
    if (v[i][j] == n)
      return make_pair(i, j);
  }
  return make_pair(-1, -1);
}

void view(vvi const &v) {
  puts("v---");
  rep(i, v.size()) {
    rep(j, v[i].size()) { cout << v[i][j] << " "; }
    cout << endl;
  }
  puts("^---");
}

int main() {
  int m;
  while (cin >> m && m) {
    int a, b;
    vvi ps(m);
    rep(i, m) ps[i] = {i + 1};
    while (cin >> a >> b && a | b) {
      auto ia = find(a, ps), ib = find(b, ps);
      vi &A = ps[ia.first], &B = ps[ib.first];
      if (b == 0) {
        if (ia.second == 0)
          continue;
        while (1) {
          int top = A.back();
          ps.push_back({top});
          A.pop_back();
          if (top == a)
            break;
        }
      } else if (ia.first == ib.first) {
        if (ia.second < ib.second) {
          ps.push_back({b, a});
          while (1) {
            int top = A.back();
            if (top != a && top != b) {
              ps.push_back({top});
            }
            A.pop_back();
            if (top == a)
              break;
          }
        }
      } else {
        while (1) {
          int top = A.back();
          A.pop_back();
          if (top == a) {
            B.push_back(a);
            break;
          } else {
            ps.push_back({top});
          }
        }
      }
      ps.erase(remove_if(ps.begin(), ps.end(),
                         [](const vi &v) { return v.size() == 0; }),
               ps.end());
    }
    vi ans;
    rep(i, ps.size()) if (ps[i].size()) ans.push_back(ps[i].size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
    cout << "end" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

pair<int, int> find(int n, vvi const &v) {
  rep(i, v.size()) rep(j, v[i].size()) {
    if (v[i][j] == n)
      return make_pair(i, j);
  }
  return make_pair(-1, -1);
}

void view(vvi const &v) {
  puts("v---");
  rep(i, v.size()) {
    rep(j, v[i].size()) { cout << v[i][j] << " "; }
    cout << endl;
  }
  puts("^---");
}

int main() {
  int m;
  while (cin >> m && m) {
    int a, b;
    vvi ps(m);
    rep(i, m) ps[i] = {i + 1};
    while (cin >> a >> b && a | b) {
      auto ia = find(a, ps), ib = find(b, ps);
      vi &A = ps[ia.first], &B = ps[ib.first];
      if (b == 0) {
        if (ia.second == 0)
          continue;
        while (1) {
          int top = A.back();
          ps.push_back({top});
          A.pop_back();
          if (top == a)
            break;
        }
      } else if (ia.first == ib.first) {
        if (ia.second < ib.second) {
          ps.push_back({b, a});
          while (1) {
            int top = A.back();
            if (top != a && top != b) {
              ps.push_back({top});
            }
            A.pop_back();
          }
        }
      } else {
        while (1) {
          int top = A.back();
          A.pop_back();
          if (top == a) {
            B.push_back(a);
            break;
          } else {
            ps.push_back({top});
          }
        }
      }
      for (int i = 0; i < ps.size(); i++) {
        if (ps[i].size() == 0)
          ps.erase(ps.begin() + i);
      }
    }
    vi ans;
    rep(i, ps.size()) if (ps[i].size()) ans.push_back(ps[i].size());
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
    cout << "end" << endl;
  }
}