Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = modpow(x, n / 2);
  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

ll power(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = power(x, n / 2);
  if (n % 2 == 0)
    return res * res;
  else
    return res * res * x;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

/* <--------------------------------------------> */

typedef tuple<ll, ll, ll> T;
// get<K>(tuple型の変数)

// 有向グラフでの閉路検出
// O(V+E)
const int NMAX = 1001010;
vector<int> G[NMAX];
vector<int> status(NMAX, 0);
vector<int> dp(NMAX, -1);
// 0:New, 1:Active, 2:Finished
// Activeは行きがけ順序、Finishedは帰り掛け順序

bool IsAcyclicDFS(int v) {
  status[v] = 1;
  for (auto to : G[v]) {
    if (status[to] == 1)
      return false;
    else if (status[to] == 0) {
      if (IsAcyclicDFS(to) == false)
        return false;
    }
  }
  status[v] = 2;
  return true;
}

bool IsAcyclic() {
  for (int i = 0; i < NMAX; ++i)
    status[i] = 0;
  for (int i = 0; i < NMAX; ++i) {
    if (status[i] == 0) {
      if (IsAcyclicDFS(i) == false)
        return false;
    }
  }
  return true;
}

int rec(int now) {
  if (dp[now] != -1)
    return dp[now];

  int res = 1;
  for (auto to : G[now]) {
    chmax(res, rec(to) + 1);
  }
  return dp[now] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1, 0));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n - 1; ++j) {
      cin >> a[i][j];
      --a[i][j];
    }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 2; ++j) {
      int s1 = i, t1 = a[i][j];
      int s2 = i, t2 = a[i][j + 1];
      if (s1 > t1)
        swap(s1, t1);
      if (s2 > t2)
        swap(s2, t2);
      G[1000 * s1 + t1].push_back(1000 * s2 + t2);
    }
  }

  if (IsAcyclic() == false) {
    cout << -1 << endl;
  }

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      chmax(ans, rec(1000 * i + j));
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    ごちゃごちゃ場合分けを考える前に全探索は考えましたか？
*/

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = modpow(x, n / 2);
  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

ll power(ll x, ll n) {
  if (n == 0)
    return 1;

  ll res = power(x, n / 2);
  if (n % 2 == 0)
    return res * res;
  else
    return res * res * x;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

/* <--------------------------------------------> */

typedef tuple<ll, ll, ll> T;
// get<K>(tuple型の変数)

// 有向グラフでの閉路検出
// O(V+E)
const int NMAX = 1001010;
vector<int> G[NMAX];
vector<int> status(NMAX, 0);
vector<int> dp(NMAX, -1);
// 0:New, 1:Active, 2:Finished
// Activeは行きがけ順序、Finishedは帰り掛け順序

bool IsAcyclicDFS(int v) {
  status[v] = 1;
  for (auto to : G[v]) {
    if (status[to] == 1)
      return false;
    else if (status[to] == 0) {
      if (IsAcyclicDFS(to) == false)
        return false;
    }
  }
  status[v] = 2;
  return true;
}

bool IsAcyclic() {
  for (int i = 0; i < NMAX; ++i)
    status[i] = 0;
  for (int i = 0; i < NMAX; ++i) {
    if (status[i] == 0) {
      if (IsAcyclicDFS(i) == false)
        return false;
    }
  }
  return true;
}

int rec(int now) {
  if (dp[now] != -1)
    return dp[now];

  int res = 1;
  for (auto to : G[now]) {
    chmax(res, rec(to) + 1);
  }
  return dp[now] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1, 0));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n - 1; ++j) {
      cin >> a[i][j];
      --a[i][j];
    }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 2; ++j) {
      int s1 = i, t1 = a[i][j];
      int s2 = i, t2 = a[i][j + 1];
      if (s1 > t1)
        swap(s1, t1);
      if (s2 > t2)
        swap(s2, t2);
      G[1000 * s1 + t1].push_back(1000 * s2 + t2);
    }
  }

  if (IsAcyclic() == false) {
    cout << -1 << endl;
    return 0;
  }

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      chmax(ans, rec(1000 * i + j));
    }
  }
  cout << ans << endl;

  return 0;
}