Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> a(n, vector<int>(n - 1));
  for (auto &i : a)
    for (auto &j : i) {
      scanf("%d", &j);
      j--;
    }
  vector<int> outdeg(n * n + 5, 0);
  vector<vector<int>> adj(n * n + 5);
  vector<int> indeg(n * n + 5, 0);
  vector<vector<int>> cadj(n * n + 5);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      for (int k = j + 1; k < n - 1; k++) {
        int l1 = min(i, a[i][j]), l2 = max(i, a[i][j]);
        int p1 = min(i, a[i][k]), p2 = max(i, a[i][k]);
        adj[l1 * n + l2].push_back(p1 * n + p2);
        cadj[p1 * n + p2].push_back(l1 * n + l2);
        indeg[p1 * n + p2]++;
        outdeg[l1 * n + l2]++;
      }
    }
  }

  // int ind = -1;
  queue<int> q;
  for (int i = 0; i < (n * n); i++) {
    int sec = i % n, fir = i / n;
    if (fir >= sec)
      continue;
    if (indeg[i] == 0) {
      q.push(i);
    }
  }

  int vis = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis++;
    for (int v : adj[u]) {
      indeg[v]--;
      if (indeg[v] == 0)
        q.push(v);
    }
  }

  if (vis != (n * (n - 1) / 2)) {
    printf("-1\n");
    return 0;
  }

  vector<int> d(n * n + 5, 0);

  for (int i = 0; i < (n * n); i++) {
    int sec = i % n, fir = i / n;
    if (fir >= sec)
      continue;
    if (outdeg[i] == 0) {
      d[i] = 1;
      q.push(i);
    }
  }

  int cnt = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    cnt++;
    for (int v : cadj[u]) {
      outdeg[v]--;
      d[v] = max(d[v], d[u] + 1);
      if (outdeg[v] == 0)
        q.push(v);
    }
  }
  // cerr << "cnt = " << cnt << endl;
  printf("%d\n", *max_element(d.begin(), d.end()));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<int>> a(n, vector<int>(n - 1));
  for (auto &i : a)
    for (auto &j : i) {
      scanf("%d", &j);
      j--;
    }
  vector<int> outdeg(n * n + 5, 0);
  vector<vector<int>> adj(n * n + 5);
  vector<int> indeg(n * n + 5, 0);
  vector<vector<int>> cadj(n * n + 5);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      // for (int k=j+1; k<n-1; k++) {
      int k = j + 1;
      if (k >= (n - 1))
        continue;
      int l1 = min(i, a[i][j]), l2 = max(i, a[i][j]);
      int p1 = min(i, a[i][k]), p2 = max(i, a[i][k]);
      adj[l1 * n + l2].push_back(p1 * n + p2);
      cadj[p1 * n + p2].push_back(l1 * n + l2);
      indeg[p1 * n + p2]++;
      outdeg[l1 * n + l2]++;
      // }
    }
  }

  // int ind = -1;
  queue<int> q;
  for (int i = 0; i < (n * n); i++) {
    int sec = i % n, fir = i / n;
    if (fir >= sec)
      continue;
    if (indeg[i] == 0) {
      q.push(i);
    }
  }

  int vis = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis++;
    for (int v : adj[u]) {
      indeg[v]--;
      if (indeg[v] == 0)
        q.push(v);
    }
  }

  if (vis != (n * (n - 1) / 2)) {
    printf("-1\n");
    return 0;
  }

  vector<int> d(n * n + 5, 0);

  for (int i = 0; i < (n * n); i++) {
    int sec = i % n, fir = i / n;
    if (fir >= sec)
      continue;
    if (outdeg[i] == 0) {
      d[i] = 1;
      q.push(i);
    }
  }

  int cnt = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    cnt++;
    for (int v : cadj[u]) {
      outdeg[v]--;
      d[v] = max(d[v], d[u] + 1);
      if (outdeg[v] == 0)
        q.push(v);
    }
  }
  // cerr << "cnt = " << cnt << endl;
  printf("%d\n", *max_element(d.begin(), d.end()));
}