Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = pow(10, 9) + 7;
// const ll MOD = 998244353;
// const ll MOD = ;
ll mod(ll A, ll M) { return (A % M + M) % M; }
const ll INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll divceil(ll A, ll B) { return (A + (B - 1)) / B; }
ll myctoi(char C) { return C - '0'; }
char myitoc(ll N) { return '0' + N; }
#define FINALANS(A)                                                            \
  {                                                                            \
    cout << (A) << '\n';                                                       \
    exit(0);                                                                   \
  }

int main() {
  clock_t start = clock();

  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      scanf("%d", &a);
      A.at(i).at(j) = a - 1;
    }
  }

  vector<int> B(N, 0);
  for (int ans = 1;; ans++) {
    if (clock_t() / 1000 >= 1800)
      FINALANS(-1);

    vector<bool> visited(N, false);
    bool ng = true;
    for (int i = 0; i < N; i++) {
      if (visited.at(i))
        continue;
      if (B.at(i) == N - 1)
        continue;

      int x = A.at(i).at(B.at(i));

      if (visited.at(x))
        continue;
      if (B.at(x) == N - 1)
        continue;

      int y = A.at(x).at(B.at(x));

      if (i == y) {
        B.at(i)++;
        B.at(x)++;
        visited.at(i) = true;
        visited.at(x) = true;
        ng = false;
      }
    }

    if (ng)
      FINALANS(-1);

    bool ok = true;
    for (int i = 0; i < N; i++) {
      if (B.at(i) < N - 1) {
        ok = false;
        break;
      }
    }
    if (ok)
      FINALANS(ans);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = pow(10, 9) + 7;
// const ll MOD = 998244353;
// const ll MOD = ;
ll mod(ll A, ll M) { return (A % M + M) % M; }
const ll INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll divceil(ll A, ll B) { return (A + (B - 1)) / B; }
ll myctoi(char C) { return C - '0'; }
char myitoc(ll N) { return '0' + N; }
#define FINALANS(A)                                                            \
  {                                                                            \
    cout << (A) << '\n';                                                       \
    exit(0);                                                                   \
  }

int main() {
  clock_t start = clock();

  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      scanf("%d", &a);
      A.at(i).at(j) = a - 1;
    }
  }

  vector<int> B(N, 0);
  for (int ans = 1;; ans++) {
    if (clock() / 1000 >= 1900)
      FINALANS(N * (N - 1) / 2);

    vector<bool> visited(N, false);
    bool ng = true;
    for (int i = 0; i < N; i++) {
      if (visited.at(i))
        continue;
      if (B.at(i) == N - 1)
        continue;

      int x = A.at(i).at(B.at(i));

      if (visited.at(x))
        continue;
      if (B.at(x) == N - 1)
        continue;

      int y = A.at(x).at(B.at(x));

      if (i == y) {
        B.at(i)++;
        B.at(x)++;
        visited.at(i) = true;
        visited.at(x) = true;
        ng = false;
      }
    }

    if (ng)
      FINALANS(-1);

    bool ok = true;
    for (int i = 0; i < N; i++) {
      if (B.at(i) < N - 1) {
        ok = false;
        break;
      }
    }
    if (ok)
      FINALANS(ans);
  }
}