Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
/**Function Template**/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for (ll i = ll(0); i < ll(n); i++)
#define Rep(i, n) for (ll i = ll(1); i < ll(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.14159265358979323846
#define ifYN(x) cout << (x ? "Yes" : "No") << "\n"
ll fac[MAX], finv[MAX], inv[MAX];

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

bool palindrome(string s) {
  bool flag = true;
  rep(i, (ll)s.size()) if (s[i] != s[s.size() - 1 - i]) flag = false;
  return flag;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll Len(ll n) {
  ll s = 0;
  while (n != 0)
    s++, n /= 10;
  return s;
}

ll Sint(ll n) {
  ll ans = 0;
  while (n != 0)
    ans += n % 10, n /= 10;
  return ans;
}

ll Svec(vector<ll> v) {
  ll n = 0;
  rep(i, (ll)v.size()) n += v[i];
  return n;
}

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

ll Factorial(ll n) {
  ll m = 1;
  while (n >= 1)
    m *= n, n--;
  return m;
}

void runlength(string s, vector<pair<char, ll>> &p) {
  ll x = 1;
  if (s.size() == 1) {
    p.push_back(pair<char, ll>(s[0], 1));
  }
  rep(i, (ll)s.size() - 1) {
    if (s[i] == s[i + 1]) {
      x++;
      if (i == (ll)s.size() - 2) {
        p.push_back(pair<char, ll>(s[i], x));
      }
    } else {
      p.push_back(pair<char, ll>(s[i], x));
      x = 1;
      if (i == (ll)s.size() - 2) {
        p.push_back(pair<char, ll>(s[s.size() - 1], x));
      }
    }
  }
}

ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

string Toupper(string s) {
  string ans = "";
  rep(i, s.size()) {
    if ('a' <= s[i] && s[i] <= 'z')
      ans += (char)s[i] - 32;
    else
      ans += s[i];
  }
  return ans;
}
string Tolower(string s) {
  string ans = "";
  rep(i, s.size()) {
    if ('A' <= s[i] && s[i] <= 'Z')
      ans += (char)s[i] + 32;
    else
      ans += s[i];
  }
  return ans;
}

void dis(vector<ll> v) { rep(i, v.size()) cout << v[i] << endl; }

void dis2(vector<vector<ll>> v) {
  rep(i, v.size()) {
    rep(j, v[0].size()) cout << v[i][j] << ' ';
    cout << endl;
  }
}

const int MAX_N = 100010;
vector<bool> sieve_of_eratosthenes() {
  vector<bool> isPrime(MAX_N + 1, true);
  /* isPrime[1]=false; */
  for (int i = 2; i <= MAX_N; i++) {
    if (isPrime[i]) {
      for (int j = 2 * i; j <= MAX_N; j += i) {
        isPrime[j] = false;
      }
    }
  }
  return isPrime;
}

vector<pint> prime_factorize(ll n) {
  vector<pint> ans;
  for (ll p = 2; p <= sqrt(n); p++) {
    if (n % p != 0)
      continue;
    ll cnt = 0;
    while (n % p == 0) {
      n /= p;
      cnt++;
    }
    ans.push_back(make_pair(p, cnt));
  }
  if (n != 1)
    ans.push_back(make_pair(n, 1));
  return ans;
}

/*bool cmp(pint a, pint b) { return a.second < b.second; }*/

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

ll encode(ll i, ll j) {
  if (i < j)
    swap(i, j);
  return i * (i - 1) / 2 + j + 1;
}

ll seen[2000000]; // 1:現在探索中のパスで行きがけ 2:探索後の頂点
ll d[2000000];    // d[i]:= 始点から i への距離
ll dfs(vector<vector<ll>> g, ll n) {
  if (seen[n] == 2)
    return d[n];
  seen[n] = 1;
  for (auto c : g[n]) {
    if (seen[c] == 1) { // 閉路を検出
      cout << -1 << endl;
      exit(0);
    }
    chmax(d[n], dfs(g, c) + 1);
  }
  seen[n] = 2;
  return d[n];
}
int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> g(n * (n - 1) / 2 + 1);
  rep(i, n) {
    ll now = 0;
    rep(_, n - 1) {
      ll j;
      cin >> j;
      j--;
      ll next = encode(i, j);
      g[now].push_back(next);
      now = next;
    }
  }
  cout << dfs(g, 0) << endl;
}

Fixed submission:
/**Function Template**/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for (ll i = ll(0); i < ll(n); i++)
#define Rep(i, n) for (ll i = ll(1); i < ll(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.14159265358979323846
#define ifYN(x) cout << (x ? "Yes" : "No") << "\n"
ll fac[MAX], finv[MAX], inv[MAX];

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

bool palindrome(string s) {
  bool flag = true;
  rep(i, (ll)s.size()) if (s[i] != s[s.size() - 1 - i]) flag = false;
  return flag;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll Len(ll n) {
  ll s = 0;
  while (n != 0)
    s++, n /= 10;
  return s;
}

ll Sint(ll n) {
  ll ans = 0;
  while (n != 0)
    ans += n % 10, n /= 10;
  return ans;
}

ll Svec(vector<ll> v) {
  ll n = 0;
  rep(i, (ll)v.size()) n += v[i];
  return n;
}

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

ll Factorial(ll n) {
  ll m = 1;
  while (n >= 1)
    m *= n, n--;
  return m;
}

void runlength(string s, vector<pair<char, ll>> &p) {
  ll x = 1;
  if (s.size() == 1) {
    p.push_back(pair<char, ll>(s[0], 1));
  }
  rep(i, (ll)s.size() - 1) {
    if (s[i] == s[i + 1]) {
      x++;
      if (i == (ll)s.size() - 2) {
        p.push_back(pair<char, ll>(s[i], x));
      }
    } else {
      p.push_back(pair<char, ll>(s[i], x));
      x = 1;
      if (i == (ll)s.size() - 2) {
        p.push_back(pair<char, ll>(s[s.size() - 1], x));
      }
    }
  }
}

ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

string Toupper(string s) {
  string ans = "";
  rep(i, s.size()) {
    if ('a' <= s[i] && s[i] <= 'z')
      ans += (char)s[i] - 32;
    else
      ans += s[i];
  }
  return ans;
}
string Tolower(string s) {
  string ans = "";
  rep(i, s.size()) {
    if ('A' <= s[i] && s[i] <= 'Z')
      ans += (char)s[i] + 32;
    else
      ans += s[i];
  }
  return ans;
}

void dis(vector<ll> v) { rep(i, v.size()) cout << v[i] << endl; }

void dis2(vector<vector<ll>> v) {
  rep(i, v.size()) {
    rep(j, v[0].size()) cout << v[i][j] << ' ';
    cout << endl;
  }
}

const int MAX_N = 100010;
vector<bool> sieve_of_eratosthenes() {
  vector<bool> isPrime(MAX_N + 1, true);
  /* isPrime[1]=false; */
  for (int i = 2; i <= MAX_N; i++) {
    if (isPrime[i]) {
      for (int j = 2 * i; j <= MAX_N; j += i) {
        isPrime[j] = false;
      }
    }
  }
  return isPrime;
}

vector<pint> prime_factorize(ll n) {
  vector<pint> ans;
  for (ll p = 2; p <= sqrt(n); p++) {
    if (n % p != 0)
      continue;
    ll cnt = 0;
    while (n % p == 0) {
      n /= p;
      cnt++;
    }
    ans.push_back(make_pair(p, cnt));
  }
  if (n != 1)
    ans.push_back(make_pair(n, 1));
  return ans;
}

/*bool cmp(pint a, pint b) { return a.second < b.second; }*/

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

ll encode(ll i, ll j) {
  if (i < j)
    swap(i, j);
  return i * (i - 1) / 2 + j + 1;
}

ll seen[2000000]; // 1:現在探索中のパスで行きがけ 2:探索後の頂点
ll d[2000000];    // d[i]:= 始点から i への距離
ll dfs(vector<vector<ll>> &g, ll n) {
  if (seen[n] == 2)
    return d[n];
  seen[n] = 1;
  for (auto c : g[n]) {
    if (seen[c] == 1) { // 閉路を検出
      cout << -1 << endl;
      exit(0);
    }
    chmax(d[n], dfs(g, c) + 1);
  }
  seen[n] = 2;
  return d[n];
}
int main() {
  ll n;
  cin >> n;
  vector<vector<ll>> g(n * (n - 1) / 2 + 1);
  rep(i, n) {
    ll now = 0;
    rep(_, n - 1) {
      ll j;
      cin >> j;
      j--;
      ll next = encode(i, j);
      g[now].push_back(next);
      now = next;
    }
  }
  cout << dfs(g, 0) << endl;
}