Language: C++

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

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
//const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
//const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
#include <queue>
#define INF INT_MAX
typedef long long ll;
struct edge{ll to, cost;};
typedef pair<int,int> P;
int n;

struct graph{
  ll V;
  vector<vector<edge> > G;
  vector<int> d;

  graph(int n){
    init(n);
  }

  void init(int n){
    V = n;
    G.resize(V);
    d.resize(V);
    for(int i = 0; i < V; i++){
      d[i] = INF;
    }
  }

  void add_edge(int s, int t, int cost){
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }

  void dijkstra(int s){
    for(int i = 0; i < V; i++){
      d[i] = INF;
    }
    d[s] = 0;
    priority_queue<P,vector<P>, greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()){
      P p = que.top(); que.pop();
      int v = p.second;
      if(d[v]<p.first) continue;
      // if(d[v] < -2*(n-1)) return false;
      for(auto e : G[v]){
        if(d[e.to]>d[v]+e.cost){
          d[e.to] = d[v]+e.cost;
          que.push(P(d[e.to],e.to));
        }
      }
    }
    // return true;
  }

};

struct StronglyConnectedComponents
{
  int n;
  vector<vector<int>> oG, rG;
  vector<int> comp, order, used;
  vector<pair<int, int>> edges;

  StronglyConnectedComponents (int n) : n(n), oG(n), rG(n), comp(n, -1), used(n, 0) {}

  int operator[] (int k)
  {
    return comp[k];
  }

  void add_edge(int u, int v)
  {
    oG[u].push_back(v);
    rG[v].push_back(u);
    edges.emplace_back(u, v);
  }

  void dfs(int v)
  {
    if (used[v]) return;
    used[v] = true;
    for (int to : oG[v]) dfs(to);
    order.push_back(v);
  }

  void rdfs(int v, int k)
  {
    if (~comp[v]) return;
    comp[v] = k;
    for (int to : rG[v]) rdfs(to, k);
  }

  void build(vector<vector<int>> &t)
  {
    for (int i = 0; i < n; ++i) dfs(i);

    reverse(order.begin(), order.end());
    int k = 0;
    for (int i : order) if (comp[i] == -1) rdfs(i, k++);

    t.resize(k);
    set<pair<int, int>> connect;
    for (auto &e : edges) {
      int u = comp[e.first], v = comp[e.second];
      if (u == v || connect.count({u, v})) continue;
      t[u].push_back(v);
      connect.emplace(u, v);
    }
  }
};

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;

  VVI a(n,VI(n-1));
  map<PII,int> mp;

  rep(i,n) rep(j,n-1) {
    cin >> a[i][j];
    a[i][j]--;
    mp[PII(i,a[i][j])] = j;
  }

  StronglyConnectedComponents g2(n * (n-1) + 1);
  graph g( n * (n-1) + 1);

  rep(i,n)rep(j,n-2){
    g.add_edge(i*(n-1)+j, i*(n-1)+j+1,-1);
    g2.add_edge(i*(n-1)+j, i*(n-1)+j+1);
  }

  rep(i,n)rep(j,n-1){
    int idx = a[i][j];
    int idx2 = mp[PII(idx,i)] ;
    g.add_edge(i*(n-1)+j, idx*(n-1) + idx2, 0);
    g2.add_edge(i*(n-1)+j, idx*(n-1) + idx2);

  }
  rep(i,n){
    g.add_edge(n*(n-1),i*(n-1),-1);
    g2.add_edge(n*(n-1),i*(n-1));
  }
  vector<vector<int>> G;
  g2.build(G);
  if(G.size() != n*(n-1)/2 + 1){
    //cout << G.size() << endl;
    cout << -1 << endl;
    return 0;
  }
  g.dijkstra(n*(n-1));
  if(!flag){
    cout << -1 << endl;
    return 0;
  }
  int ans = 0;
  rep(i,n){
    ans = max<int>(-g.d[ i*(n-1) + (n-1) - 1], ans);
  }
  cout << ans << endl;
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:182:7: error: 'flag' was not declared in this scope
  182 |   if(!flag){
      |       ^~~~