Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

#define maxv 1000 * (1000 - 1) / 2 + 1

int dp[1005];
vector<vector<int>> graph;
vector<int> visited, cal;
int dfs(int v) { // 最長経路
  if (visited[v]) {
    if (!cal[v])
      return -1;
    return dp[v];
  }
  visited[v] = true;
  dp[v] = 1;
  for (auto x : graph[v]) {
    int res = dfs(x);
    if (res == -1)
      return -1;
    dp[v] = max(dp[v], res + 1);
  }
  cal[v] = 1;
  return dp[v]; // トポロジカルソートはメモ化再帰と相性がよい
}
signed main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i, n) rep(j, n - 1) {
    cin >> a[i][j];
    a[i][j]--;
  }
  int v = 0;
  vector<vector<int>> id(n, vector<int>(n));
  graph.resize(maxv);
  rep(i, n) rep(j, n) {
    if (i < j)
      id[i][j] = v++;
  }
  rep(i, n) {
    rep(j, n - 1) {
      if (i < a[i][j])
        a[i][j] = id[i][a[i][j]];
      else
        a[i][j] = id[a[i][j]][i];
    }
    rep(j, n - 2) graph[a[i][j + 1]].pb(
        a[i][j]); // 前後関係は同じ選手のスケジュールに依存
  }
  int ans = 0;
  visited.resize(maxv, 0);
  cal.resize(maxv, 0);
  rep(i, v) {
    int res = dfs(i);
    if (res == -1) { // resが-1のとき、ループがあり実現不可
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
using ll = long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

#define maxv 1000 * (1000 - 1) / 2 + 1

int dp[maxv];
vector<vector<int>> graph;
vector<int> visited, cal;
int dfs(int v) { // 最長経路
  if (visited[v]) {
    if (!cal[v])
      return -1;
    return dp[v];
  }
  visited[v] = true;
  dp[v] = 1;
  for (auto x : graph[v]) {
    int res = dfs(x);
    if (res == -1)
      return -1;
    dp[v] = max(dp[v], res + 1);
  }
  cal[v] = 1;
  return dp[v]; // トポロジカルソートはメモ化再帰と相性がよい
}
signed main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i, n) rep(j, n - 1) {
    cin >> a[i][j];
    a[i][j]--;
  }
  int v = 0;
  vector<vector<int>> id(n, vector<int>(n));
  graph.resize(maxv);
  rep(i, n) rep(j, n) {
    if (i < j)
      id[i][j] = v++;
  }
  rep(i, n) {
    rep(j, n - 1) {
      if (i < a[i][j])
        a[i][j] = id[i][a[i][j]];
      else
        a[i][j] = id[a[i][j]][i];
    }
    rep(j, n - 2) graph[a[i][j + 1]].pb(
        a[i][j]); // 前後関係は同じ選手のスケジュールに依存
  }
  int ans = 0;
  visited.resize(maxv, 0);
  cal.resize(maxv, 0);
  rep(i, v) {
    int res = dfs(i);
    if (res == -1) { // resが-1のとき、ループがあり実現不可
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}