Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define dom 998244353
#define all(c) begin(c), end(c)
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define long long long
#define List vector
#define var auto
#define Count size()
#define Length size()
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

int A[1024][1024];
int B[1024];
bool U[1024];
void solve() {
  int N;
  cin >> N;
  var A = vector<vector<int>>(N, vector<int>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> A[i][j];
      A[i][j]--;
    }
  }
  int okcnt = 0;

  int cnt = 0;
  while (okcnt < N) {
    rep(i, N) U[i] = false;
    bool flag = false;
    for (int i = 0; i < N; i++) {
      if (U[i])
        continue;
      if (B[i] == N - 1)
        continue;

      int rival = A[i][B[i]];
      if (U[rival])
        continue;

      if (A[rival][B[rival]] == i) {
        U[i] = true;
        U[rival] = true;
        flag = true;
        B[i]++;
        B[rival]++;

        if (B[rival] == N - 1)
          okcnt++;
        if (B[i] == N - 1)
          okcnt++;
      }
    }

    if (!flag) {
      cout << ("-1") << endl;
      return;
    }
    cnt++;
  }

  cout << (cnt) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define dom 998244353
#define all(c) begin(c), end(c)
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define long long long
#define List vector
#define var auto
#define Count size()
#define Length size()
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

int A[1024][1024];
int B[1024];
bool U[1024];
void solve() {
  int N;
  cin >> N;
  var A = vector<vector<int>>(N, vector<int>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> A[i][j];
      A[i][j]--;
    }
  }
  int okcnt = 0;

  int cnt = 0;
  while (okcnt < N) {
    if (cnt > N * (N - 1) / 2) {
      cout << ("-1") << endl;
      return;
    }
    rep(i, N) U[i] = false;
    bool flag = false;
    for (int i = 0; i < N; i++) {
      if (U[i])
        continue;
      if (B[i] == N - 1)
        continue;

      int rival = A[i][B[i]];
      if (U[rival])
        continue;

      if (A[rival][B[rival]] == i) {
        U[i] = true;
        U[rival] = true;
        flag = true;
        B[i]++;
        B[rival]++;

        if (B[rival] == N - 1)
          okcnt++;
        if (B[i] == N - 1)
          okcnt++;
      }
    }

    if (!flag) {
      cout << ("-1") << endl;
      return;
    }
    cnt++;
  }

  cout << (cnt) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}