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

#include <algorithm>
#include <cstdio>
#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)

vvi v;

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

int main() {
  int m;
  while (scanf("%d", &m), m) {
    int a, b;
    v.assign(m, {0});
    rep(i, m) v[i] = {i + 1};
    while (scanf("%d%d", &a, &b), a | b) {
      auto ia = find(a), ib = find(b);
      vi &A = v[ia.first], &B = v[ib.first];
      if (b == 0) {
        if (ia.second == 0)
          continue;
        while (1) {
          int top = A.back();
          v.push_back({top});
          A.pop_back();
          if (top == a)
            break;
        }
      } else if (ia.first == ib.first) {
        if (ia.second < ib.second) {
          v.push_back({b, a});
          while (1) {
            int top = A.back();
            if (top != a && top != b) {
              v.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 {
            v.push_back({top});
          }
        }
      }
      v.erase(remove_if(v.begin(), v.end(),
                        [](const vi &v) { return v.size() == 0; }),
              v.end());
    }
    vi ans;
    rep(i, v.size()) if (v[i].size()) ans.push_back(v[i].size());
    sort(ans.begin(), ans.end());
    rep(i, ans.size()) printf("%d\\n", ans[i]);
    puts("end");
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#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);
}

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());
    rep(i, ans.size()) cout << ans[i] << endl;
    cout << "end" << endl;
  }
}