Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <cmath>
#include <utility> // pair make_pair
#include <algorithm>
#include <iomanip>
#include <climits> // LLONG_MAX, LLONG_MIN, INT_MIN, INT_MAX
#include <assert.h>

#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define ll long long
//#define int ll
#define INF INT_MAX/10

using namespace std;
using Pi = pair<int, int>;
using Graph = vector<vector<int> >;
using WeightGraph = vector<vector<Pi> >;

using namespace std;
 
class DAG {
  int n;
  vector<vector<int>> adj;
  vector<int> visited;
 
  void dfs(int v) {
    if (visited[v] == 2) {
      is_acyclic = false;
      return;
    } else if (!visited[v]) {
      visited[v] = 2;
      for (int s : adj[v]) {
        dfs(s);
      }
      visited[v] = 1;
      sorted.push_back(v);
    }
  }
 
public:
  vector<int> sorted;
  DAG(int n) : n(n), adj(n), visited(n) {}
  DAG(vector<vector<int>> &adj) : n(adj.size()), adj(adj), visited(adj.size()) {}
  bool is_acyclic = true;
 
  void add_arc(int a, int b) {
    assert(0 <= a && a < n && 0 <= b && b < n);
    adj[a].push_back(b);
  }
 
  void tsort() {
    for (int i = 0; i < n; i++) {
      if (!visited[i]) dfs(i);
    }
    reverse(sorted.begin(), sorted.end());
  }
};
 
int longest_path(const vector<vector<int>> &adj) {
  int n = adj.size();
  vector<vector<int>> inv(n);
  for (int a = 0; a < n; a++) {
    for (auto b : adj[a]) {
      inv[b].push_back(a);
    }
  }
  vector<int> len(n, -1);
  function<void(int)> set_len = [&](int v) {
                                  if (len[v] != -1) return;
                                  if (inv[v].empty()) {
                                    len[v] = 0;
                                    return;
                                  }
                                  int nax = 0;
                                  for (int u : inv[v]) {
                                    set_len(u);
                                    nax = max(nax, len[u]);
                                  }
                                  len[v] = nax + 1;
                                };
 
  for (int i = 0; i < n; i++) {
    set_len(i);
  }
 
  return *max_element(len.begin(), len.end());
}
 
int main() {
  int n;
  cin >> n;
 
  vector<vector<int>> adj(n * (n - 1));
  auto get = [&](int a, int b) {
               if (a > b) swap(a, b);
               return a * n + b;
             };
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    int prev = get(i, a);
    for (int j = 0; j < n - 2; j++) {
      int b;
      cin >> b;
      b--;
      int cur = get(i, b);
      //            cout << prev << ' ' << cur << endl;
      adj[prev].push_back(cur);
      prev = cur;
    }
  }
 
  DAG dag(adj);
  dag.tsort();
  if (!dag.is_acyclic) {
    cout << -1 << endl;
    return 0;
  }
 
  cout << longest_path(adj) + 1 << endl;
}


Compiler output:
a.cc: In function 'int longest_path(const std::vector<std::vector<int> >&)':
a.cc:78:3: error: 'function' was not declared in this scope
   78 |   function<void(int)> set_len = [&](int v) {
      |   ^~~~~~~~
a.cc:16:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
   15 | #include <assert.h>
  +++ |+#include <functional>
   16 | 
a.cc:78:12: error: expected primary-expression before 'void'
   78 |   function<void(int)> set_len = [&](int v) {
      |            ^~~~
a.cc:93:5: error: 'set_len' was not declared in this scope; did you mean 'strlen'?
   93 |     set_len(i);
      |     ^~~~~~~
      |     strlen