Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define int long long int
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 3e6 + 100;
vector<int> g[N];
vector<int> in[N];
ll ind[N];

ll dis[N];

ll dp(ll v) {
  ll ma = 0;
  for (int i = 0; i < in[v].size(); i++) {
    ll u = in[v][i];
    if (dis[u] > 1e9) {
      dp(u);
    }
    ma = max(ma, dis[u] + 1);
  }
  dis[v] = ma;
}
int32_t main() {
  sync;
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    ll z = i;
    if (x < i) {
      swap(x, z);
    }
    ll cnt = z * 1000 + x;
    for (int j = 0; j < n - 2; j++) {
      ll x;
      cin >> x;
      ll z = i;
      if (x < i)
        swap(x, z);
      g[cnt].pb(z * 1000 + x);
      in[z * 1000 + x].pb(cnt);
      ind[z * 1000 + x]++;
      cnt = z * 1000 + x;
    }
  }
  memset(dis, 69, sizeof dis);
  queue<int> q;
  for (int i = 1; i < N; i++) {
    if (ind[i] == 0) {
      q.push(i);
      dis[i] = 0;
    }
  }
  while (q.size()) {
    ll v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      ll u = g[v][i];
      if (dis[u] > dis[v] + 1) {
        dis[u] = dis[v] + 1;
        q.push(u);
      }
    }
  }
  ll ma = 0;
  for (int i = 1; i < N; i++) {
    ma = max(ma, dis[i]);
  }
  if (ma > 1e9) {
    cout << -1 << endl;
    return 0;
  }
  memset(dis, 69, sizeof dis);
  ma = 0;

  for (int i = 1; i < N; i++) {
    if (dis[i] > 1e9) {
      dp(i);
      ma = max(ma, dis[i]);
    }
  }
  cout << ma + 1;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define int long long int
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

const int N = 3e6 + 100;
vector<int> g[N];
vector<int> in[N];
ll ind[N];

ll dis[N];

ll dp(ll v) {
  ll ma = 0;
  for (int i = 0; i < in[v].size(); i++) {
    ll u = in[v][i];
    if (dis[u] > 1e9) {
      dp(u);
    }
    ma = max(ma, dis[u] + 1);
  }
  dis[v] = ma;
}
int32_t main() {
  sync;
  ll n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    ll z = i;
    if (x < i) {
      swap(x, z);
    }
    ll cnt = z * 1000 + x;
    for (int j = 0; j < n - 2; j++) {
      ll x;
      cin >> x;
      ll z = i;
      if (x < i)
        swap(x, z);
      g[cnt].pb(z * 1000 + x);
      in[z * 1000 + x].pb(cnt);
      ind[z * 1000 + x]++;
      cnt = z * 1000 + x;
    }
  }
  memset(dis, 69, sizeof dis);
  queue<int> q;
  for (int i = 1; i < N; i++) {
    if (ind[i] == 0) {
      q.push(i);
      dis[i] = 0;
    }
  }
  while (q.size()) {
    ll v = q.front();
    q.pop();
    for (int i = 0; i < g[v].size(); i++) {
      ll u = g[v][i];
      ind[u]--;
      if (ind[u] == 0) {
        dis[u] = 0;
        q.push(u);
      }
    }
  }
  ll ma = 0;
  for (int i = 1; i < N; i++) {
    ma = max(ma, dis[i]);
  }
  if (ma > 1e9) {
    cout << -1 << endl;
    return 0;
  }
  memset(dis, 69, sizeof dis);
  ma = 0;

  for (int i = 1; i < N; i++) {
    if (dis[i] > 1e9) {
      dp(i);
      ma = max(ma, dis[i]);
    }
  }
  cout << ma + 1;
}