Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18)
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define PP pair<P, P>
#define all(v) v.begin(), v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define vecunique(vec)                                                         \
  sort(vec.begin(), vec.end());                                                \
  decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());        \
  vec.erase(result, vec.end())
using namespace std;
template <class T> inline void chmax(T &a, T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = min(a, b); }

bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return x != 1;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
  random_device rnd;
  mt19937_64 mt64(rnd());
  uniform_int_distribution<int> engine(MIN, MAX);
  return engine(mt64);
}
/*--------Library Zone!--------*/

int n;
int a[1010][1010];
vector<int> v[100010];
int g[100010];
int cost[100010];
queue<int> que;
signed main() {
  cin >> n;
  rep(i, n) {
    rep(j, n - 1) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  rep(i, n) {
    rep(j, n - 2) {
      int k = a[i][j], l = i;
      int k2 = a[i][j + 1], l2 = i;
      if (l > k)
        swap(l, k);
      if (l2 > k2)
        swap(l2, k2);
      v[l * n + k].push_back(l2 * n + k2);
      g[l2 * n + k2]++;
    }
  }
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (!g[i * n + j]) {
        que.push(i * n + j);
        cost[i * n + j] = 1;
      }
    }
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (auto q : v[p]) {
      g[q]--;
      if (!g[q]) {
        que.push(q);
        cost[q] = cost[p] + 1;
      }
    }
  }
  int ma = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (!cost[i * n + j]) {
        cout << -1 << endl;
        return 0;
      }
      chmax(ma, cost[i * n + j]);
    }
  }
  cout << ma << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18)
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define PP pair<P, P>
#define all(v) v.begin(), v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define vecunique(vec)                                                         \
  sort(vec.begin(), vec.end());                                                \
  decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());        \
  vec.erase(result, vec.end())
using namespace std;
template <class T> inline void chmax(T &a, T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = min(a, b); }

bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return x != 1;
}
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
  random_device rnd;
  mt19937_64 mt64(rnd());
  uniform_int_distribution<int> engine(MIN, MAX);
  return engine(mt64);
}
/*--------Library Zone!--------*/

int n;
int a[1010][1010];
vector<int> v[1000100];
int g[1000010];
int cost[1000010];
queue<int> que;
signed main() {
  cin >> n;
  rep(i, n) {
    rep(j, n - 1) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  rep(i, n) {
    rep(j, n - 2) {
      int k = a[i][j], l = i;
      int k2 = a[i][j + 1], l2 = i;
      if (l > k)
        swap(l, k);
      if (l2 > k2)
        swap(l2, k2);
      v[l * n + k].push_back(l2 * n + k2);
      g[l2 * n + k2]++;
    }
  }
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (!g[i * n + j]) {
        que.push(i * n + j);
        cost[i * n + j] = 1;
      }
    }
  }
  while (!que.empty()) {
    int p = que.front();
    que.pop();
    for (auto q : v[p]) {
      g[q]--;
      if (!g[q]) {
        que.push(q);
        cost[q] = cost[p] + 1;
      }
    }
  }
  int ma = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (!cost[i * n + j]) {
        cout << -1 << endl;
        return 0;
      }
      chmax(ma, cost[i * n + j]);
    }
  }
  cout << ma << endl;
}