Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// おまじない
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
// 型alias
template <class T> using vec = vector<T>;
using vint = vec<int>;
using vvint = vec<vint>;
using str = string;
using mstr = map<str, int>;
using mchar = map<char, int>;
using mint = map<int, int>;
// alias
#define gcd __gcd
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
signed main() {
  INCANT;
  int n, res = 0, a;
  cin >> n;
  queue<int> q[1111];
  rep(i, n) {
    rep(j, n - 1) {
      cin >> a;
      a--;
      q[i].push(a);
    }
  }
  int tmp = n * (n - 1) / 2;
  for (; tmp > 0; res++) {
    bool flag = true;
    bool used[1111] = {};
    rep(i, n) {
      if (q[i].empty()) {
        continue;
      }
      int p = q[i].front();
      if (!used[i] && !used[p] && q[p].front() == i) {
        used[i] = used[p] = true;
        q[i].pop();
        q[p].pop();
        tmp--;
        flag = false;
      }
    }
    if (flag) {
      cout << -1;
      return 0;
    }
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// おまじない
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(false),                        \
      cout << fixed << setprecision(20);
#define int long long
// 型alias
template <class T> using vec = vector<T>;
using vint = vec<int>;
using vvint = vec<vint>;
using str = string;
using mstr = map<str, int>;
using mchar = map<char, int>;
using mint = map<int, int>;
// alias
#define gcd __gcd
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for (auto &&i : n)
signed main() {
  INCANT;
  int n, res = 0, a;
  cin >> n;
  queue<int> q[1111];
  rep(i, n) {
    rep(j, n - 1) {
      cin >> a;
      a--;
      q[i].push(a);
    }
  }
  int tmp = n * (n - 1) / 2;
  for (; tmp > 0; res++) {
    bool flag = true;
    bool used[1111] = {};
    rep(i, n) {
      if (q[i].empty()) {
        continue;
      }
      while (1) {
        int p = q[i].front();
        if (!used[i] && !used[p] && q[p].front() == i) {
          used[i] = used[p] = true;
          q[i].pop();
          q[p].pop();
          tmp--;
          flag = false;
        } else {
          break;
        }
      }
    }
    if (flag) {
      cout << -1;
      return 0;
    }
  }
  cout << res;
}