Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define M 1000000007
#define U 998244353
#define N 1000005
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int dp[1001][1000];
int a[1001][1000];
int ainv[1001][1001];
signed main() {
  ios_base::sync_with_stdio(0);
  int TESTS = 1;
  cin.tie(0);
  cout.tie(0);
  // cin>>TESTS;
  while (TESTS--) {
    int n;
    cin >> n;
    rep(i, 1, n + 1) { rep(j, 1, n) cin >> a[i][j]; }
    int cur[n + 1];
    rep(i, 1, n + 1) cur[i] = 1;
    int ans = 0;
    while (1) {
      int cnt = 0;
      ans++;
      bitset<1001> mark = 0;
      rep(i, 1, n + 1) {
        if (cur[i] == n || mark[i])
          continue;
        if (a[a[i][cur[i]]][cur[a[i][cur[i]]]] == i &&
            mark[a[i][cur[i]]] == 0) {
          cnt++;
          mark[i] = 1;
          mark[a[i][cur[i]]] = 1;
          // trace(i,a[i][cur[i]],ans);
          cur[a[i][cur[i]]]++;
          cur[i]++;
        }
      }
      if (cnt == 0) {
        bool yes = 1;
        rep(i, 1, n + 1) if (cur[i] != n) yes = 0;
        if (yes == 0)
          cout << -1;
        else {
          cout << ans - 1;
        }
        r0;
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
// #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define M 1000000007
#define U 998244353
#define N 1000005
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int dp[1001][1000];
int a[1001][1000];
int ainv[1001][1001];
signed main() {
  ios_base::sync_with_stdio(0);
  int TESTS = 1;
  cin.tie(0);
  cout.tie(0);
  // cin>>TESTS;
  while (TESTS--) {
    int n;
    cin >> n;
    rep(i, 1, n + 1) { rep(j, 1, n) cin >> a[i][j]; }
    int cur[n + 1];
    rep(i, 1, n + 1) cur[i] = 1;
    int ans = 0;
    while (1) {
      int cnt = 0;
      ans++;
      bool mark[n + 1] = {};
      rep(i, 1, n + 1) {
        if (cur[i] == n || mark[i])
          continue;
        if (a[a[i][cur[i]]][cur[a[i][cur[i]]]] == i &&
            mark[a[i][cur[i]]] == 0) {
          cnt++;
          mark[i] = 1;
          mark[a[i][cur[i]]] = 1;
          // trace(i,a[i][cur[i]],ans);
          cur[a[i][cur[i]]]++;
          cur[i]++;
        }
      }
      if (cnt == 0) {
        bool yes = 1;
        rep(i, 1, n + 1) if (cur[i] != n) yes = 0;
        if (yes == 0)
          cout << -1;
        else {
          cout << ans - 1;
        }
        r0;
      }
    }
  }
}