Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(name) name.begin(), name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

int N;
vector<vector<int>> A;

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  A.assign(N, vector<int>(N, 0));
  REP(i, N) {
    REP(j, N - 1) {
      cin >> A[i][j];
      A[i][j]--;
    }
  }

  // 各 A がどれだけ進んでいるか
  vector<int> prog(N, 0);
  // 最後までたどり着いたものの数
  int cnt = 0;

  vector<int> check;
  REP(i, N) { check.push_back(i); }

  int ans = 0;
  bool isChange = true;
  while (isChange) {
    isChange = false;
    ans++;

    vector<int> node_to(N, -1); // i の指し先
    vector<int> next_check;
    for (auto &i : check) {
      if (prog[i] >= N - 1) {
        continue;
      }

      node_to[i] = A[i][prog[i]];
      if (node_to[node_to[i]] == i) {
        prog[i]++;
        if (prog[i] >= N - 1) {
          cnt++;
        }
        prog[node_to[i]]++;
        if (prog[node_to[i]] >= N - 1) {
          cnt++;
        }

        node_to[node_to[i]] = -1;
        node_to[i] = -1;
        isChange = true;
      }
    }
    for (auto &i : check) {
      if (prog[i] >= N - 1) {
        continue;
      }
      next_check.push_back(i);
    }
    check.clear();
    copy(next_check.begin(), next_check.end(), back_inserter(check));
  }

  ans--;
  if (cnt < N) {
    ans = -1;
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(name) name.begin(), name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

int N;
vector<vector<int>> A;

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  A.assign(N, vector<int>(N, 0));
  REP(i, N) {
    REP(j, N - 1) {
      cin >> A[i][j];
      A[i][j]--;
    }
  }

  // 各 A がどれだけ進んでいるか
  vector<int> prog(N, 0);
  // 最後までたどり着いたものの数
  int cnt = 0;

  vector<int> check;
  REP(i, N) { check.push_back(i); }

  int ans = 0;
  bool isChange = true;
  while (isChange) {
    isChange = false;
    ans++;

    vector<int> node_to(N, -1); // i の指し先
    vector<int> next_check;
    for (auto &i : check) {
      if (prog[i] >= N - 1) {
        continue;
      }

      node_to[i] = A[i][prog[i]];
      if (node_to[node_to[i]] == i) {
        prog[i]++;
        if (prog[i] >= N - 1) {
          cnt++;
        }
        prog[node_to[i]]++;
        if (prog[node_to[i]] >= N - 1) {
          cnt++;
        }

        node_to[node_to[i]] = -1;
        node_to[i] = -1;
        isChange = true;
      }
    }
    for (auto &i : check) {
      if (prog[i] >= N - 1) {
        continue;
      }
      next_check.push_back(i);
    }
    check.clear();
    copy(next_check.begin(), next_check.end(), back_inserter(check));

    if (ans > 100000) {
      printf("%lld\n", N * (N - 1) / 2);
      return 0;
    }
  }

  ans--;
  if (cnt < N) {
    ans = -1;
  }
  printf("%lld\n", ans);

  return 0;
}