Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, e) for (int i = a; i < e; ++i)
using LL = long long;

struct TopologicalSort {
  vector<vector<int>> E;
  TopologicalSort(int N) { E.resize(N); }
  void add_edge(int a, int b) { E[a].push_back(b); }
  bool visit(int v, vector<int> &order, vector<int> &color) {
    color[v] = 1;
    for (int u : E[v]) {
      if (color[u] == 2)
        continue;
      if (color[u] == 1)
        return false;
      if (!visit(u, order, color))
        return false;
    }
    order.push_back(v);
    color[v] = 2;
    return true;
  }
  bool sort(vector<int> &order) {
    int n = E.size();
    vector<int> color(n);
    for (int u = 0; u < n; u++)
      if (!color[u] && !visit(u, order, color))
        return false;
    reverse(order.begin(), order.end());
    return true;
  }
};
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
vector<int> E[101010];
int dp[101010];
int main() {
  int N;
  cin >> N;
  // 閉路検出+DAGの最長パス
  using PII = pair<int, int>;
  int c = 0;
  map<PII, int> id;
  {
    FOR(i, 0, N) {
      FOR(j, i + 1, N) { id[PII(i, j)] = c++; }
    }
  }
  TopologicalSort G(c);
  FOR(i, 0, N) {
    vector<int> b(N - 1);
    FOR(j, 0, N - 1) {
      cin >> b[j];
      b[j]--;
    }
    FOR(j, 0, N - 2) {
      int from = id[i < b[j] ? PII(i, b[j]) : PII(b[j], i)];
      int to = id[i < b[j + 1] ? PII(i, b[j + 1]) : PII(b[j + 1], i)];
      G.add_edge(from, to);
      E[from].push_back(to);
    }
  }
  vector<int> ord;
  bool ok = G.sort(ord);
  if (!ok) {
    cout << "-1" << endl;
    return 0;
  } else {
    fore(cu, ord) fore(to, E[cu]) chmax(dp[to], dp[cu] + 1);
    int ans = 0;
    rep(i, 0, c) chmax(ans, dp[i]);
    cout << ans + 1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, e) for (int i = a; i < e; ++i)
using LL = long long;

struct TopologicalSort {
  vector<vector<int>> E;
  TopologicalSort(int N) { E.resize(N); }
  void add_edge(int a, int b) { E[a].push_back(b); }
  bool visit(int v, vector<int> &order, vector<int> &color) {
    color[v] = 1;
    for (int u : E[v]) {
      if (color[u] == 2)
        continue;
      if (color[u] == 1)
        return false;
      if (!visit(u, order, color))
        return false;
    }
    order.push_back(v);
    color[v] = 2;
    return true;
  }
  bool sort(vector<int> &order) {
    int n = E.size();
    vector<int> color(n);
    for (int u = 0; u < n; u++)
      if (!color[u] && !visit(u, order, color))
        return false;
    reverse(order.begin(), order.end());
    return true;
  }
};
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
vector<int> E[1000 * 500];
int dp[1000 * 500];
int main() {
  int N;
  cin >> N;
  // 閉路検出+DAGの最長パス
  using PII = pair<int, int>;
  int c = 0;
  map<PII, int> id;
  {
    FOR(i, 0, N) {
      FOR(j, i + 1, N) { id[PII(i, j)] = c++; }
    }
  }
  TopologicalSort G(c);
  FOR(i, 0, N) {
    vector<int> b(N - 1);
    FOR(j, 0, N - 1) {
      cin >> b[j];
      b[j]--;
    }
    FOR(j, 0, N - 2) {
      int from = id[i < b[j] ? PII(i, b[j]) : PII(b[j], i)];
      int to = id[i < b[j + 1] ? PII(i, b[j + 1]) : PII(b[j + 1], i)];
      G.add_edge(from, to);
      E[from].push_back(to);
    }
  }
  vector<int> ord;
  bool ok = G.sort(ord);
  if (!ok) {
    cout << "-1" << endl;
    return 0;
  } else {
    fore(cu, ord) fore(to, E[cu]) chmax(dp[to], dp[cu] + 1);
    int ans = 0;
    rep(i, 0, c) chmax(ans, dp[i]);
    cout << ans + 1 << endl;
  }
}