Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define PB push_back
#define MP make_pair
#define DEBUG printf("%s\n", "debug")
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define SCD(n) scanf("%d", &n)
#define SCD2(m, n) scanf("%d%d", &m, &n)
#define SCD3(m, n, k) scanf("%d%d%d", &m, &n, &k)
#define SCLLD(n) scanf("%lld", &n)
#define SCLLD2(m, n) scanf("%lld%lld", &m, &n)
#define SCLLD3(m, n, k) scanf("%lld%lld%lld", &m, &n, &k)
using namespace std;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const ll MOD = 1e9 + 7;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      v += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
    return is >> x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

///////////////////////////////////////////////
/*(._.)*/
int INF = 1e9;
static vector<PII> E[1022][1022];
int indeg[1022][1022] = {};
int len[1022][1022] = {};
bool visited[1022][1022] = {};
bool cycle = false;
int mcnt = 10000000;
int cnt = 0;
int dfs(int i, int j) {
  cnt++;
  if (mcnt < cnt) {
    cycle = true;
    return 0;
  }

  visited[i][j] = true;
  if (len[i][j] != 0) {
    // visited[i][j] = false;
    return len[i][j];
  }

  int siz = E[i][j].size();
  if (siz == 0) {
    visited[i][j] = false;
    return 0;
  } else {
    int ret = 0;
    REP(k, siz) {
      if (mcnt < cnt) {
        cycle = true;
        return 0;
      }

      if (visited[E[i][j][k].fst][E[i][j][k].snd])
        cycle = true;
      ret = max(ret, 1 + dfs(E[i][j][k].fst, E[i][j][k].snd));
    }
    visited[i][j] = false;
    return len[i][j] = ret;
  }
}

int main() {
  int N;
  cin >> N;

  vector<vector<int>> A(N + 200);
  REP(i, N) {
    REP(j, N - 1) {
      int a;
      cin >> a;
      A[i + 1].PB(a);
    }
  }

  FORq(i, 1, N) {
    REP(j, N - 2) {
      int nowi = min(i, A[i][j]);
      int nowj = max(i, A[i][j]);
      int nexti = min(i, A[i][j + 1]);
      int nextj = max(i, A[i][j + 1]);
      E[nowi][nowj].PB(MP(nexti, nextj));
      // indeg[nexti][nextj]++;
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      sort(E[i][j].begin(), E[i][j].end());
      E[i][j].erase(unique(E[i][j].begin(), E[i][j].end()), E[i][j].end());
      for (auto p : E[i][j]) {
        indeg[p.first][p.second]++;
      }
    }
  }
  // initilize
  FORq(i, 1, N) {
    FORq(j, 1, N) {
      if (i >= j)
        continue;
      if (indeg[i][j] == 0) {
        E[0][0].PB(MP(i, j));
      }
    }
  }
  int ans = dfs(0, 0);
  if ((cycle) or (ans == 0))
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define PB push_back
#define MP make_pair
#define DEBUG printf("%s\n", "debug")
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define SCD(n) scanf("%d", &n)
#define SCD2(m, n) scanf("%d%d", &m, &n)
#define SCD3(m, n, k) scanf("%d%d%d", &m, &n, &k)
#define SCLLD(n) scanf("%lld", &n)
#define SCLLD2(m, n) scanf("%lld%lld", &m, &n)
#define SCLLD3(m, n, k) scanf("%lld%lld%lld", &m, &n, &k)
using namespace std;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const ll MOD = 1e9 + 7;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      v += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
    return is >> x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

///////////////////////////////////////////////
/*(._.)*/
int INF = 1e9;
static vector<PII> E[1022][1022];
int indeg[1022][1022] = {};
int len[1022][1022] = {};
bool visited[1022][1022] = {};
bool cycle = false;
int mcnt = 10000000;
int cnt = 0;
int dfs(int i, int j) {
  cnt++;
  if (mcnt < cnt) {
    cycle = true;
    return 0;
  }
  if (cycle)
    return 0;
  visited[i][j] = true;
  if (len[i][j] != 0) {
    // visited[i][j] = false;
    return len[i][j];
  }

  int siz = E[i][j].size();
  if (siz == 0) {
    visited[i][j] = false;
    return 0;
  } else {
    int ret = 0;
    REP(k, siz) {
      if (mcnt < cnt) {
        cycle = true;
        return 0;
      }

      if (visited[E[i][j][k].fst][E[i][j][k].snd])
        cycle = true;
      ret = max(ret, 1 + dfs(E[i][j][k].fst, E[i][j][k].snd));
    }
    visited[i][j] = false;
    return len[i][j] = ret;
  }
}

int main() {
  int N;
  cin >> N;

  vector<vector<int>> A(N + 200);
  REP(i, N) {
    REP(j, N - 1) {
      int a;
      cin >> a;
      A[i + 1].PB(a);
    }
  }

  FORq(i, 1, N) {
    REP(j, N - 2) {
      int nowi = min(i, A[i][j]);
      int nowj = max(i, A[i][j]);
      int nexti = min(i, A[i][j + 1]);
      int nextj = max(i, A[i][j + 1]);
      E[nowi][nowj].PB(MP(nexti, nextj));
      // indeg[nexti][nextj]++;
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      sort(E[i][j].begin(), E[i][j].end());
      E[i][j].erase(unique(E[i][j].begin(), E[i][j].end()), E[i][j].end());
      for (auto p : E[i][j]) {
        indeg[p.first][p.second]++;
      }
    }
  }
  // initilize
  FORq(i, 1, N) {
    FORq(j, 1, N) {
      if (i >= j)
        continue;
      if (indeg[i][j] == 0) {
        E[0][0].PB(MP(i, j));
      }
    }
  }
  int ans = dfs(0, 0);
  if ((cycle) or (ans == 0))
    ans = -1;
  cout << ans << endl;
}