Language: C++

include <bits/stdc++.h>
 
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:1:1: error: 'include' does not name a type
    1 | include <bits/stdc++.h>
      | ^~~~~~~
a.cc:7:5: error: 'vector' does not name a type
    7 |     vector<vector<int>> adj;
      |     ^~~~~~
a.cc:8:5: error: 'vector' does not name a type
    8 |     vector<int> visited;
      |     ^~~~~~
a.cc:25:5: error: 'vector' does not name a type
   25 |     vector<int> sorted;
      |     ^~~~~~
a.cc:27:15: error: expected ')' before '<' token
   27 |     DAG(vector<vector<int>> &adj) : n(adj.size()), adj(adj), visited(adj.size()) {}
      |        ~      ^
      |               )
a.cc: In member function 'void DAG::dfs(int)':
a.cc:11:13: error: 'visited' was not declared in this scope
   11 |         if (visited[v] == 2) {
      |             ^~~~~~~
a.cc:16:26: error: 'adj' was not declared in this scope
   16 |             for (int s : adj[v]) {
      |                          ^~~
a.cc:20:13: error: 'sorted' was not declared in this scope
   20 |             sorted.push_back(v);
      |             ^~~~~~
a.cc: In constructor 'DAG::DAG(int)':
a.cc:26:24: error: class 'DAG' does not have any field named 'adj'
   26 |     DAG(int n) : n(n), adj(n), visited(n) {}
      |                        ^~~
a.cc:26:32: error: class 'DAG' does not have any field named 'visited'
   26 |     DAG(int n) : n(n), adj(n), visited(n) {}
      |                                ^~~~~~~
a.cc: In member function 'void DAG::add_arc(int, int)':
a.cc:31:9: error: 'assert' was not declared in this scope
   31 |         assert(0 <= a && a < n && 0 <= b && b < n);
      |         ^~~~~~
a.cc:1:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
  +++ |+#include <cassert>
    1 | include <bits/stdc++.h>
a.cc:32:9: error: 'adj' was not declared in this scope
   32 |         adj[a].push_back(b);
      |         ^~~
a.cc: In member function 'void DAG::tsort()':
a.cc:37:18: error: 'visited' was not declared in this scope
   37 |             if (!visited[i]) dfs(i);
      |                  ^~~~~~~
a.cc:39:17: error: 'sorted' was not declared in this scope
   39 |         reverse(sorted.begin(), sorted.end());
      |                 ^~~~~~
a.cc:39:9: error: 'reverse' was not declared in this scope
   39 |         reverse(sorted.begin(), sorted.end());
      |         ^~~~~~~
a.cc: At global scope:
a.cc:43:24: error: 'vector' does not name a type
   43 | int longest_path(const vector<vector<int>> &adj) {
      |                        ^~~~~~
a.cc:43:30: error: expected ',' or '...' before '<' token
   43 | int longest_path(const vector<vector<int>> &adj) {
      |                              ^
a.cc: In function 'int longest_path(int)':
a.cc:44:13: error: 'adj' was not declared in this scope
   44 |     int n = adj.size();
      |             ^~~
a.cc:45:5: error: 'vector' was not declared in this scope
   45 |     vector<vector<int>> inv(n);
      |     ^~~~~~
a.cc:45:19: error: expected primary-expression before 'int'
   45 |     vector<vector<int>> inv(n);
      |                   ^~~
a.cc:48:13: error: 'inv' was not declared in this scope; did you mean 'int'?
   48 |             inv[b].push_back(a);
      |             ^~~
      |             int
a.cc:51:12: error: expected primary-expression before 'int'
   51 |     vector<int> len(n, -1);
      |            ^~~
a.cc:52:5: error: 'function' was not declared in this scope; did you mean 'union'?
   52 |     function<void(int)> set_len = [&](int v) {
      |     ^~~~~~~~
      |     union
a.cc:52:14: error: expected primary-expression before 'void'
   52 |     function<void(int)> set_len = [&](int v) {
      |              ^~~~
a.cc:67:9: error: 'set_len' was not declared in this scope
   67 |         set_len(i);
      |         ^~~~~~~
a.cc:70:25: error: 'len' was not declared in this scope
   70 |     return *max_element(len.begin(), len.end());
      |                         ^~~
a.cc:70:13: error: 'max_element' was not declared in this scope
   70 |     return *max_element(len.begin(), len.end());
      |             ^~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:75:5: error: 'cin' was not declared in this scope
   75 |     cin >> n;
      |     ^~~
a.cc:77:5: error: 'vector' was not declared in this scope
   77 |     vector<vector<int>> adj(n * (n - 1));
      |     ^~~~~~
a.cc:77:19: error: expected primary-expression before 'int'
   77 |     vector<vector<int>> adj(n * (n - 1));
      |                   ^~~
a.cc: In lambda function:
a.cc:79:20: error: 'swap' was not declared in this scope
   79 |         if (a > b) swap(a, b);
      |                    ^~~~
a.cc: In function 'int main()':
a.cc:93:13: error: 'adj' was not declared in this scope
   93 |             adj[prev].push_back(cur);
      |             ^~~
a.cc:98:13: error: 'adj' was not declared in this scope
   98 |     DAG dag(adj);
      |             ^~~
a.cc:101:9: error: 'cout' was not declared in this scope
  101 |         cout << -1 << endl;
      |         ^~~~
a.cc:101:23: error: 'endl' was not declared in this scope
  101 |         cout << -1 << endl;
      |                       ^~~~
a.cc:105:5: error: 'cout' was not declared in this scope
  105 |     cout << longest_path(adj) + 1 << endl;
      |     ^~~~
a.cc:105:38: error: 'endl' was not declared in this scope
  105 |     cout << longest_path(adj) + 1 << endl;
      |                                      ^~~~