Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

const int MAXN = 1000;
const int MAXV = MAXN * (MAXN - 1) / 2;
vector<vector<int>> to(MAXV);

vector<int> dp(MAXV);
vector<bool> visited(MAXV, false);
vector<bool> calculated(MAXV, false);

int dfs(int v) {
  // cout << "dfs" << v << dp[v] << endl;
  if (visited[v]) {
    if (!calculated[v]) {
      // find roop!
      return -1;
    }
    // return dp[v];
  }
  if (calculated[v])
    return dp[v];

  visited[v] = true;
  int res = 1;
  for (int p : to[v]) {
    int tmp = dfs(p);
    if (tmp == -1)
      return -1;
    res = max(res, tmp + 1);
  }
  dp[v] = res;
  calculated[v] = true;
  return dp[v];
}

int 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]--;
    }
  }

  // 試合組み合わせにidをつける
  vector<vector<int>> id(n, vector<int>(n - 1));
  int matchId = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      id[i][j] = matchId++;
    }
  }

  rep(i, n) {
    for (int j = 0; j < i; j++) {
      id[i][j] = id[j][i];
    }
  }
  /*
  rep(i,n){
          rep(j,n){
                  printf("%d%c", id[i][j], j==n-1? '\n':' ');
          }
  }
  */

  rep(i, n) {
    rep(j, n - 1) {
      int from = a[i][j];
      a[i][j] = id[i][from];
      // printf("%d%c", a[i][j], j==n-2? '\n':' ');
    }
  }

  rep(i, n) {
    rep(j, n - 2) { to[a[i][j]].push_back(a[i][j + 1]); }
  }
  /*
  rep(i,matchId){
          rep(j,to[i].size()){
                  printf("%d:%d%c", i,to[i][j], j==to[i].size()-1? '\n':' ');
          }
  }
  */

  rep(i, matchId) {
    int tmp = dfs(i);
    if (tmp == -1) {
      puts("-1");
      return 0;
    }
  }

  int res = 0;
  rep(i, dp.size()) res = max(res, dp[i]);
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

const int MAXN = 1000;
const int MAXV = MAXN * (MAXN - 1) / 2;
vector<vector<int>> to(MAXV);

vector<int> dp(MAXV);
vector<bool> visited(MAXV, false);
vector<bool> calculated(MAXV, false);

int dfs(int v) {
  // cout << "dfs" << v << dp[v] << endl;
  if (visited[v]) {
    if (!calculated[v]) {
      // find roop!
      return -1;
    }
    // return dp[v];
  }
  if (calculated[v])
    return dp[v];

  visited[v] = true;
  int res = 1;
  for (int p : to[v]) {
    int tmp = dfs(p);
    if (tmp == -1)
      return -1;
    res = max(res, tmp + 1);
  }
  dp[v] = res;
  calculated[v] = true;
  return dp[v];
}

int 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]--;
    }
  }

  // 試合組み合わせにidをつける
  vector<vector<int>> id(n, vector<int>(n));
  int matchId = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      id[i][j] = matchId++;
    }
  }

  rep(i, n) {
    for (int j = 0; j < i; j++) {
      id[i][j] = id[j][i];
    }
  }
  /*
  rep(i,n){
          rep(j,n){
                  printf("%d%c", id[i][j], j==n-1? '\n':' ');
          }
  }
  */

  rep(i, n) {
    rep(j, n - 1) {
      int from = a[i][j];
      a[i][j] = id[i][from];
      // printf("%d%c", a[i][j], j==n-2? '\n':' ');
    }
  }

  rep(i, n) {
    rep(j, n - 2) { to[a[i][j]].push_back(a[i][j + 1]); }
  }
  /*
  rep(i,matchId){
          rep(j,to[i].size()){
                  printf("%d:%d%c", i,to[i][j], j==to[i].size()-1? '\n':' ');
          }
  }
  */

  rep(i, matchId) {
    int tmp = dfs(i);
    if (tmp == -1) {
      puts("-1");
      return 0;
    }
  }

  int res = 0;
  rep(i, dp.size()) res = max(res, dp[i]);
  cout << res << endl;
  return 0;
}