Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define COUTDOUBLE(d, i) cout << setprecision((i)) << (d) << endl;
#define VIN(v)                                                                 \
  for (int64_t i = 0; i < (v).size(); i++) {                                   \
    cin >> (v).at(i);                                                          \
  }
#define MREP(i, m)                                                             \
  for (auto i = (m).begin(); i != (m).end();                                   \
       ++i) // auto key = i->first;auto value = i->second;

using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;

I MOD = pow(10, 9) + 7;
I MAX = LONG_MAX;
I MIN = LONG_MIN;

// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }

// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }

int ctoi(char c) { return c - '0'; }

I a[1010][1010];
I c[1010]; // どこまで終わったか

int main() {
  I n;
  cin >> n;
  REP(i, n + 1) {
    if (i == 0)
      continue;
    REP(j, n) {
      if (j == 0)
        continue;
      cin >> a[i][j];
    }
  }
  I d = 1;
  V l;
  REP(i, n + 1) {
    if (i == 0)
      continue;
    l.push_back(i);
  }
  while (true) {
    V v;
    bool chk = true;
    REP(j, l.size()) {
      I i = l.at(j);
      if (i == a[a[i][c[i] + 1]][c[a[i][c[i] + 1]] + 1]) {
        v.push_back(i);
        v.push_back(a[i][c[i] + 1]);
        chk = false;
      }
    }
    if (chk) {
      break;
    }
    map<I, I> mp;
    REP(i, v.size()) {
      if (mp[v.at(i)] != -1) {
        c[v.at(i)]++;
        mp[v.at(i)] = -1;
      }
    }
    d++;
    l = v;
  }
  REP(i, n + 1) {
    if (i == 0)
      continue;
    if (c[i] != n - 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << d - 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define COUTDOUBLE(d, i) cout << setprecision((i)) << (d) << endl;
#define VIN(v)                                                                 \
  for (int64_t i = 0; i < (v).size(); i++) {                                   \
    cin >> (v).at(i);                                                          \
  }
#define MREP(i, m)                                                             \
  for (auto i = (m).begin(); i != (m).end();                                   \
       ++i) // auto key = i->first;auto value = i->second;

using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;

I MOD = pow(10, 9) + 7;
I MAX = LONG_MAX;
I MIN = LONG_MIN;

// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }

// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }

int ctoi(char c) { return c - '0'; }

I a[1010][1010];
I c[1010]; // どこまで終わったか

int main() {
  I n;
  cin >> n;
  REP(i, n + 1) {
    if (i == 0)
      continue;
    REP(j, n) {
      if (j == 0)
        continue;
      cin >> a[i][j];
    }
  }
  I d = 1;
  V l;
  REP(i, n + 1) {
    if (i == 0)
      continue;
    l.push_back(i);
  }
  while (true) {
    V v;
    bool chk = true;
    REP(j, l.size()) {
      I i = l.at(j);
      if (i == a[a[i][c[i] + 1]][c[a[i][c[i] + 1]] + 1]) {
        v.push_back(i);
        v.push_back(a[i][c[i] + 1]);
        chk = false;
      }
    }
    if (chk) {
      break;
    }
    SORT(v);
    v.erase(unique(ALL(v)), v.end());
    REP(i, v.size()) { c[v.at(i)]++; }
    d++;
    l = v;
  }
  REP(i, n + 1) {
    if (i == 0)
      continue;
    if (c[i] != n - 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << d - 1 << endl;
  return 0;
}