Problem p02925 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC target("avx2")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1.0);
const int C_SIZE = 3121000;
namespace {
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
long long Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
  fact[0] = finv[0] = inv[1] = 1;
  for (int i = 2; i < n; i++) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  }
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long pw(long long a, long long b) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
} // namespace
// ここから編集しろ
int p[1100][1100];
int G[1100][1100];
vector<int> g[510000];
int v[510000];
int dfs(int a) {
  if (v[a] == -2) {
    return -2;
  }
  if (v[a] != -1) {
    return v[a];
  }
  v[a] = -2;
  int ret = 0;
  for (int i = 0; i < g[a].size(); i++) {
    int tmp = dfs(g[a][i]);
    if (tmp == -2)
      return -2;
    ret = max(ret, tmp + 1);
  }
  return v[a] = ret;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a - 1; j++) {
      scanf("%d", &p[i][j]);
      p[i][j]--;
    }
  }
  int sz = 0;
  for (int i = 0; i < a; i++) {
    for (int j = 1; j < a; j++)
      G[i][j] = sz++;
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a - 2; j++) {
      int fi = G[min(i, p[i][j])][max(i, p[i][j])];
      int se = G[min(i, p[i][j + 1])][max(i, p[i][j + 1])];
      g[fi].push_back(se);
    }
  }
  for (int i = 0; i < sz; i++)
    v[i] = -1;
  int ret = 0;
  for (int i = 0; i < sz; i++) {
    if (v[i] == -1) {
      int val = dfs(i);
      if (val == -2) {
        printf("-1\n");
        return 0;
      }
      ret = max(ret, val);
    }
  }
  printf("%d\n", ret + 1);
}

Fixed submission:
// #pragma GCC target("avx2")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1.0);
const int C_SIZE = 3121000;
namespace {
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
long long Comb(int a, int b) {
  if (a < b || b < 0)
    return 0;
  return fact[a] * finv[b] % mod * finv[a - b] % mod;
}
void init_C(int n) {
  fact[0] = finv[0] = inv[1] = 1;
  for (int i = 2; i < n; i++) {
    inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
  }
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long pw(long long a, long long b) {
  if (a < 0LL)
    return 0;
  if (b < 0LL)
    return 0;
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
} // namespace
// ここから編集しろ
int p[1100][1100];
int G[1100][1100];
vector<int> g[510000];
int v[510000];
int dfs(int a) {
  if (v[a] == -2) {
    return -2;
  }
  if (v[a] != -1) {
    return v[a];
  }
  v[a] = -2;
  int ret = 0;
  for (int i = 0; i < g[a].size(); i++) {
    int tmp = dfs(g[a][i]);
    if (tmp == -2)
      return -2;
    ret = max(ret, tmp + 1);
  }
  return v[a] = ret;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a - 1; j++) {
      scanf("%d", &p[i][j]);
      p[i][j]--;
    }
  }
  int sz = 0;
  for (int i = 0; i < a; i++) {
    for (int j = i + 1; j < a; j++)
      G[i][j] = sz++;
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a - 2; j++) {
      int fi = G[min(i, p[i][j])][max(i, p[i][j])];
      int se = G[min(i, p[i][j + 1])][max(i, p[i][j + 1])];
      g[fi].push_back(se);
    }
  }
  for (int i = 0; i < sz; i++)
    v[i] = -1;
  int ret = 0;
  for (int i = 0; i < sz; i++) {
    if (v[i] == -1) {
      int val = dfs(i);
      if (val == -2) {
        printf("-1\n");
        return 0;
      }
      ret = max(ret, val);
    }
  }
  printf("%d\n", ret + 1);
}