Language: C++

//
// author  : Abhishek
//
#include <bits/stdc++.h>

#define ll long long
#define db double
#define deb(x) cout<<#x<<": "<<x<<"\n"
#define all(x) x.begin(),x.end()
#define fo(i, n) for(i=0 ; i<n ; i++)
#define Fo(i, k, n) for(i=k ; i<n ; i++)

using namespace std;

const ll mod = 1000000007;
const ll N = 100000005;  // array size
const ll inf = 10000000000000000;


void print(vector<ll> v) {
    ll i;
    fo(i, v.size())cout << v[i] << " ";
    cout << "\n";
}

vector<ll> read(ll n) {
    ll i;
    vector<ll> arr(n, 0);
    fo(i, n)cin >> arr[i];
    return arr;
}

vector<ll> read(ll n, ll k) {
    ll i;
    vector<ll> arr(n + k, 0);
    Fo(i, k, n + k)cin >> arr[i];
    return arr;
}

bool isEven(ll n) {
    return n % 2 == 0;
}

bool isOdd(ll n) {
    return n % 2 != 0;
}


void solve() {
    ll t;
    cin >> (t);
    while (t--) {

        ll i, j, k, n, m, p, q, x, y, a, b, l, r;


    }
}

map<pair<ll, ll>, ll> is;
vector<ll> adj[N];
ll ct = 0;

vector<bool> vis(N, false);

// detect cycle
bool hasCycle = false;
vector<ll> recStack(N, 0);

bool isCycle(ll cur) {
//    deb(cur);
    vis[cur] = 1;
    recStack[cur] = 1;
    for (auto next: adj[cur]) {
        if (!vis[next]) {
            if (isCycle(next)) {
                hasCycle = true;
                return true;
            }
        } else {
            if (recStack[next] == 1) {
                hasCycle = true;
                return true;
            }
        }
    }
    recStack[cur] = 0;
    return false;
}


// find max length path
vector<ll> dp(N, 0);

ll dfsFillMax(ll cur) {
    vis[cur] = 1;
    for (auto next: adj[cur]) {
        if (!vis[next]) {
            dfsFillMax(next);
        }
        dp[cur] = max(1 + dp[next], dp[cur]);
    }
    return dp[cur];
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll i, j, k, n, m, p, q, x, y, a, b, l, r;

    cin >> n;
    k = 1;

    Fo(i, 1, n + 1) {
        Fo(j, i, n + 1) {
            is[{i, j}] = k++;
            ct++;
        }
    }

//    Fo(i, 1, n + 1) {
//        Fo(j, i, n + 1) {
//            deb(i);
//            deb(j);
//            cout << is[{i, j}] << "\n";
//        }
//    }


    Fo(i, 1, n + 1) {
        ll cur;
        cin >> cur;
        fo(j, n - 2) {
            cin >> x;
//            deb(x);
            adj[is[{min(i, cur), max(i, cur)}]].push_back(is[{min(i, x), max(i, x)}]);
//            cout << is[{min(i, cur), max(i, cur)}] << " " << is[{min(i, x), max(i, x)}] << "\n";
            cur = x;
        }
    }

//    Fo(i, 1, ct + 1) {
//        cout << i << ": ";
//        print(adj[i]);
//    }


    // detect cycle
    Fo(i, 1, ct + 1) {
        if (!vis[i]) {
            isCycle(i);
        }
    }

    if (hasCycle) {
        cout << -1;
        return 0;
    }


    // find max lenth path

    Fo(i, 1, ct + 1) {
        vis[i] = 0;
    }

    Fo(i, 1, ct + 1) {
        if (!vis[i]) {
            dfsFillMax(i);
        }
    }
    ll ans = 0;
    Fo(i, 1, ct + 1) {
        ans = max(ans, dp[i]);
    }
    cout << ans + 1;


    return 0;
}




Compiler output:
/tmp/ccPVtsP5.o: in function `isCycle(long long)':
a.cc:(.text+0x2ae): relocation truncated to fit: R_X86_64_PC32 against symbol `vis' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x2dd): relocation truncated to fit: R_X86_64_PC32 against symbol `recStack' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x351): relocation truncated to fit: R_X86_64_PC32 against symbol `vis' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x38a): relocation truncated to fit: R_X86_64_PC32 against symbol `hasCycle' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x3a0): relocation truncated to fit: R_X86_64_PC32 against symbol `recStack' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x3bc): relocation truncated to fit: R_X86_64_PC32 against symbol `hasCycle' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x3f9): relocation truncated to fit: R_X86_64_PC32 against symbol `recStack' defined in .bss section in /tmp/ccPVtsP5.o
/tmp/ccPVtsP5.o: in function `dfsFillMax(long long)':
a.cc:(.text+0x42a): relocation truncated to fit: R_X86_64_PC32 against symbol `vis' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x4b0): relocation truncated to fit: R_X86_64_PC32 against symbol `vis' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x4ed): relocation truncated to fit: R_X86_64_PC32 against symbol `dp' defined in .bss section in /tmp/ccPVtsP5.o
a.cc:(.text+0x506): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status