Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define hell 1000000007
#define lcm(a, b) (a * b) / __gcd(a, b)
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll vector<pair<ll, ll>>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define F first
#define S second
#define rep(i, a, b) for (i = a; i < b; i++)
#define parr(a, n)                                                             \
  for (ll it = 0; it < n; it++)                                                \
    cout << a[it] << " ";                                                      \
  cout << endl;
#define pcont(a)                                                               \
  for (auto it : a)                                                            \
    cout << it << " ";                                                         \
  cout << endl;
#define ret(x) return cout << x, 0;
#define endl '\n'

/*constants*/
#define MAXN 1000005
#define PI 3.14159265358979323846

/*debug*/
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  std::cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

using namespace std;
vll adj[MAXN];
ll vis[MAXN], dp[MAXN];
map<pair<ll, ll>, ll> mm;
bool isCycle(ll u) {
  vis[u] = 1;
  bool fl = false;
  for (auto it : adj[u]) {
    if (vis[it] == 0)
      fl |= isCycle(it);
    else if (vis[it] == 1)
      return true;
  }
  vis[u] = 2;
  return fl;
}
void dfs(ll u) {
  vis[u] = 1;
  dp[u] = 1;
  for (auto it : adj[u]) {
    if (!vis[it]) {
      dfs(it);
    }
    dp[u] = max(dp[u], 1 + dp[it]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll i, j, n, m, k, cnt = 0, ans = 0, t = 1;
  cin >> n;
  rep(i, 1, n + 1) { rep(j, i + 1, n + 1) mm[mp(i, j)] = (++cnt); }
  set<pair<ll, ll>> e;
  rep(i, 1, n + 1) {
    pll v;
    rep(j, 0, n - 1) {
      cin >> k;
      v.pb(mp(min(i, k), max(i, k)));
    }
    rep(j, 0, v.size()) {
      rep(k, j + 1, v.size()) if (!e.count(mp(mm[v[j]], mm[v[k]])))
          adj[mm[v[j]]]
              .pb(mm[v[k]]),
          e.insert(mp(mm[v[j]], mm[v[k]]));
    }
  }
  n = cnt;
  rep(i, 1, n + 1) {
    if (!vis[i] && isCycle(i))
      ret(-1)
  }
  memset(vis, 0, sizeof(vis));
  rep(i, 1, n + 1) {
    if (!vis[i])
      dfs(i);
  }
  ret(*max_element(dp + 1, dp + n + 1));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define hell 1000000007
#define lcm(a, b) (a * b) / __gcd(a, b)
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll vector<pair<ll, ll>>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define F first
#define S second
#define rep(i, a, b) for (i = a; i < b; i++)
#define parr(a, n)                                                             \
  for (ll it = 0; it < n; it++)                                                \
    cout << a[it] << " ";                                                      \
  cout << endl;
#define pcont(a)                                                               \
  for (auto it : a)                                                            \
    cout << it << " ";                                                         \
  cout << endl;
#define ret(x) return cout << x, 0;
#define endl '\n'

/*constants*/
#define MAXN 1000005
#define PI 3.14159265358979323846

/*debug*/
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  std::cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

using namespace std;
vll adj[MAXN];
ll vis[MAXN], dp[MAXN];
map<pair<ll, ll>, ll> mm;
bool isCycle(ll u) {
  vis[u] = 1;
  bool fl = false;
  for (auto it : adj[u]) {
    if (vis[it] == 0)
      fl |= isCycle(it);
    else if (vis[it] == 1)
      return true;
  }
  vis[u] = 2;
  return fl;
}
void dfs(ll u) {
  vis[u] = 1;
  dp[u] = 1;
  for (auto it : adj[u]) {
    if (!vis[it]) {
      dfs(it);
    }
    dp[u] = max(dp[u], 1 + dp[it]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll i, j, n, m, k, cnt = 0, ans = 0, t = 1;
  cin >> n;
  rep(i, 1, n + 1) { rep(j, i + 1, n + 1) mm[mp(i, j)] = (++cnt); }
  set<pair<ll, ll>> e;
  rep(i, 1, n + 1) {
    pll v;
    rep(j, 0, n - 1) {
      cin >> k;
      v.pb(mp(min(i, k), max(i, k)));
    }
    rep(j, 0, v.size() - 1) { adj[mm[v[j]]].pb(mm[v[j + 1]]); }
  }
  n = cnt;
  rep(i, 1, n + 1) {
    if (!vis[i] && isCycle(i))
      ret(-1)
  }
  memset(vis, 0, sizeof(vis));
  rep(i, 1, n + 1) {
    if (!vis[i])
      dfs(i);
  }
  ret(*max_element(dp + 1, dp + n + 1));
  return 0;
}