Problem p00701 (C++) — buggy submission, verdict: Time Limit Exceeded

#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: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

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

int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<int>> v(n + 1);
    for (int i = 1; i < n + 1; i++)
      v[i].pb(i);

    int a, b;
    while (cin >> a >> b, a + b) {
      if (b == 0) {
        pii idxa = search_idx(a, v);
        if (idxa.first == 0)
          continue;
        vi &A = v[idxa.first];
        while (A.back() != a) {
          v[0].pb(A.back());
          A.pop_back();
        }
        v[0].pb(A.back());
        A.pop_back();
      } else {
        pii idxa = search_idx(a, v);
        pii idxb = search_idx(b, v);
        vi &A = v[idxa.first];
        vi &B = v[idxb.first];
        vector<int> &s = v[0];
        if (idxa.first != idxb.first) {
          while (A.back() != a) {
            s.pb(A.back());
            A.pop_back();
          }
          B.pb(A.back());
          A.pop_back();
        } else if (idxa.first == idxb.first && idxa.second < idxb.second) {
          vi another;
          while (A.back() != a) {
            if (A.back() == b) {
              another.pb(A.back());
              A.pop_back();
            } else {
              s.pb(A.back());
              A.pop_back();
            }
          }
          another.pb(A.back());
          A.pop_back();

          rep(i, v.size()) {
            if (i == 0)
              continue;
            if (v[i].size() == 0)
              v[i] = another;
          }
          int floor_size = v[0].size();
          rep(i, floor_size) {
            for (int j = 1; j <= n; j++) {
              if (v[j].size() == 0) {
                v[j].pb(v[0].back());
                v[0].pop_back();
                break;
              }
            }
          }
        }
      }
    }
    vi ans;
    for (int i = 0; i < v[0].size(); i++) {
      ans.pb(1);
    }

    for (int i = 1; i < v.size(); i++) {
      ans.pb(v[i].size());
    }

    sort(all(ans));

    rep(i, ans.size()) {
      if (ans[i] != 0)
        cout << ans[i] << endl;
    }
    cout << "end" << endl;
  }
}