Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    for (auto x : v)                                                           \
      cout << x << ' ';                                                        \
    cout << endl;                                                              \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    for (int _i = 0; _i < n; _i++)                                             \
      cout << *(v + _i) << ' ';                                                \
    cout << endl;                                                              \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e3 + 20;
int n;
int a[MAXN][MAXN];

int get_node(int x, int y) {
  if (x > y)
    swap(x, y);
  return x * n + y;
}

vector<int> graph[MAXN];
int tin[MAXN * MAXN], tout[MAXN * MAXN], t;
vector<int> res;
bool dfs(int node) {
  tin[node] = t++;
  for (auto to : graph[node]) {
    if (tout[to])
      continue;
    if (tin[to])
      return false;
    else {
      bool tmp = dfs(to);
      if (!tmp)
        return false;
    }
  }
  tout[node] = t++;
  res.push_back(node);
  return true;
}
bool tp_sort() {
  memset(tin, 0, sizeof(tin));
  memset(tout, 0, sizeof(tout));
  t = 1;
  for (int i = 0; i < n; i++) {
    int node = get_node(i, a[i][0]);
    if (!tin[node]) {
      bool tmp = dfs(node);
      if (!tmp)
        return false;
    }
  }
  reverse(res.begin(), res.end());
  return true;
}

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
      a[i][j]--;
      if (j)
        graph[get_node(i, a[i][j - 1])].push_back(get_node(i, a[i][j]));
    }
  }
  int valid = tp_sort();
  if (!valid) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> cur(n, 1);
  for (int i : res) {
    int x = i / n, y = i % n;
    int d = max(cur[x], cur[y]);
    cur[x] = d + 1;
    cur[y] = d + 1;
  }
  cout << *max_element(cur.begin(), cur.end()) - 1 << endl;
  // print(res);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    for (auto x : v)                                                           \
      cout << x << ' ';                                                        \
    cout << endl;                                                              \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    for (int _i = 0; _i < n; _i++)                                             \
      cout << *(v + _i) << ' ';                                                \
    cout << endl;                                                              \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e3 + 20;
int n;
int a[MAXN][MAXN];

int get_node(int x, int y) {
  if (x > y)
    swap(x, y);
  return x * n + y;
}

vector<int> graph[MAXN * MAXN];
int tin[MAXN * MAXN], tout[MAXN * MAXN], t;
vector<int> res;
bool dfs(int node) {
  tin[node] = t++;
  for (auto to : graph[node]) {
    if (tout[to])
      continue;
    if (tin[to])
      return false;
    else {
      bool tmp = dfs(to);
      if (!tmp)
        return false;
    }
  }
  tout[node] = t++;
  res.push_back(node);
  return true;
}
bool tp_sort() {
  memset(tin, 0, sizeof(tin));
  memset(tout, 0, sizeof(tout));
  t = 1;
  for (int i = 0; i < n; i++) {
    int node = get_node(i, a[i][0]);
    if (!tin[node]) {
      bool tmp = dfs(node);
      if (!tmp)
        return false;
    }
  }
  reverse(res.begin(), res.end());
  return true;
}

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
      a[i][j]--;
      if (j)
        graph[get_node(i, a[i][j - 1])].push_back(get_node(i, a[i][j]));
    }
  }
  int valid = tp_sort();
  if (!valid) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> cur(n, 1);
  for (int i : res) {
    int x = i / n, y = i % n;
    int d = max(cur[x], cur[y]);
    cur[x] = d + 1;
    cur[y] = d + 1;
  }
  cout << *max_element(cur.begin(), cur.end()) - 1 << endl;
  // print(res);

  return 0;
}