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> >;


// Input graph must be acyclic
int longest_path(const Graph &DAG) {
  int N = DAG.size();
  
  Graph Inv(N);
  for (int a = 0; a < N; a++) {
    for (auto b: DAG[a]) {
      Inv[b].push_back(a);
    }
  }

  vector<int> len(N, -1);

  function<void(int)> set_len =
    [&](int v) {
      if (len[v] != -1) return;
      // 入次数0
      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);
  }

  // 自分のノードへの距離を0からとしているので, +1する必要があるときはすること！！
  return *max_element(len.begin(), len.end());
}

// Topological sort and cycle detection
class DAG {
  int N;
  Graph G;
  vector<int> visited; // never visited 0, visited 1, visited twice 2

  void dfs(int v) {
    if (visited[v] == 2) {
      is_acyclic = false;
      return;
    } else if (!visited[v]) {
      visited[v] = 2;
      for (int nv: G[v]) {
        dfs(nv);
      }
      visited[v] = 1;
      sorted.push_back(v);
    }
  }
  
public:
  vector<int> sorted;
  DAG(int n) : N(n), G(n), visited(n) {}
  DAG(const Graph  &G) : N(G.size()), G(G), visited(G.size()) {}
  bool is_acyclic = true;
  
  void add_arc(int a, int b) {
    assert(0 <= a && a < N && 0 <= b && b < N);
    G[a].push_back(b);
  }

  void tsort() {
    for (int i = 0; i < N; i++) {
      if (!visited[i]) dfs(i);
    }
    reverse(sorted.begin(), sorted.end());
  }
};


int main() {
  int N; cin >> N;

  Graph G(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);
    // cout << "this is prev: "<< prev << endl;
    for (int j = 0; j < N - 2; j++) {
      int b; cin >> b;
      b--;
      int cur = get(i, b);
      // cout << prev << " " << cur << endl;
      G[prev].push_back(cur);
      prev = cur;
    }
  }

  DAG dag(G);
  dag.tsort();
  if (!dag.is_acyclic) {
    cout << -1 << endl;
    return 0;
  }

  cout << longest_path(G) + 1 << endl;
}


Compiler output:
a.cc: In function 'int longest_path(const Graph&)':
a.cc:43:3: error: 'function' was not declared in this scope
   43 |   function<void(int)> set_len =
      |   ^~~~~~~~
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:43:12: error: expected primary-expression before 'void'
   43 |   function<void(int)> set_len =
      |            ^~~~
a.cc:60:5: error: 'set_len' was not declared in this scope; did you mean 'strlen'?
   60 |     set_len(i);
      |     ^~~~~~~
      |     strlen