Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define rando                                                                  \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \
  cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi << "," << x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll) x.size()
#define ll long long
#define ii pair<int, int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int>>
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
queue<int> tab[1002];
int vis[1002];
int idx = 1;
int main() {
  boost;
  int n, a;
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n - 1; ++j) {
      cin >> a;
      a--;
      tab[i].push(a);
    }
  int comp = (n * (n - 1LL)) / 2LL;
  // debug(comp);
  int ans = 0;
  while (comp) {
    bool f = 0;
    for (int i = 0; i < n; ++i) {
      if (vis[i] == idx || tab[i].empty())
        continue;
      int adv = tab[i].front();
      if (vis[adv] != idx && !tab[adv].empty() && tab[adv].front() == i) {
        tab[i].pop();
        tab[adv].pop();
        vis[adv] = vis[i] = idx;
        f = 1;
        comp--;
      }
      if (!comp)
        break;
    }
    if (!f)
      break;
    ans++, idx++;
  }
  if (!comp)
    cout << ans;
  else
    cout << -1;
  return 0;
}
// long long
// array bounds
// special cases
// binary search

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define rando                                                                  \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \
  cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi << "," << x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll) x.size()
#define ll long long
#define ii pair<int, int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int>>
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
queue<int> tab[1002];
int vis[1002];
int idx = 1;
int main() {
  boost;
  int n, a;
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n - 1; ++j) {
      cin >> a;
      a--;
      tab[i].push(a);
    }
  int comp = (n * (n - 1LL)) / 2LL;
  // debug(comp);
  int ans = 0;
  while (comp) {
    bool f = 0;
    for (int i = 0; i < n; ++i) {
      if (vis[i] == idx || tab[i].empty())
        continue;
      int adv = tab[i].front();
      if (vis[adv] != idx && !tab[adv].empty() && tab[adv].front() == i) {
        tab[i].pop();
        tab[adv].pop();
        vis[adv] = vis[i] = idx;
        f = 1;
        comp--;
      }
      if (!comp)
        break;
    }
    if (!f)
      break;
    ans++, idx++;
  }
  if (!comp)
    cout << ans;
  else
    cout << -1;
  return 0;
}
// long long
// array bounds
// special cases
// binary search