Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;

constexpr int N = 1005;
constexpr int LOOPED = 1e9;

vector<vector<int>> m_no(N, vector<int>(N, -1));
vector<vector<int>> m_g(N, vector<int>(N, -1));
vector<int> len(N *N, -1);
vector<vector<int>> to(N *N, vector<int>());
vector<bool> visited(N *N, false);

int dfs(int i) {
  if (visited[i] && len[i] == -1)
    return LOOPED;

  visited[i] = true;
  if (to[i].size() == 0) {
    len[i] = 1;
  } else {
    for (auto next : to[i]) {
      int ret = dfs(next);
      if (ret == LOOPED)
        return ret;
      len[i] = max(len[i], ret + 1);
    }
  }

  return len[i];
}

int main(void) {
  IOS int n;
  cin >> n;
  vector<vector<int>> v;

  REP(i, n) {
    vector<int> v2(n - 1);
    REP(j, n - 1) {
      cin >> v2[j];
      v2[j]--;
    }
    v.emplace_back(v2);
  }

  int m_max = 0;
  REP(i, n - 1) FOR(j, i + 1, n) {
    m_no[i][j] = m_max;
    m_no[j][i] = m_max;
    m_max++;
  }

  REP(i, n) REP(j, n - 1) {
    m_g[i][j] = m_no[i][v[i][j]];
    if (j != 0)
      to[m_g[i][j - 1]].emplace_back(m_g[i][j]);
  }

  int ans = 0;
  REP(i, n) {
    ans = max(ans, dfs(m_g[i][0]));
    if (ans == LOOPED) {
      ans = -1;
      break;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;

constexpr int N = 1005;
constexpr int LOOPED = 1e9;

vector<vector<int>> m_no(N, vector<int>(N, -1));
vector<vector<int>> m_g(N, vector<int>(N, -1));
vector<int> len(N *N, -1);
vector<vector<int>> to(N *N, vector<int>());
vector<bool> visited(N *N, false);

int dfs(int i) {
  if (visited[i]) {
    if (len[i] == -1)
      return LOOPED;
    else
      return len[i];
  }

  visited[i] = true;
  if (to[i].size() == 0) {
    len[i] = 1;
  } else {
    for (auto next : to[i]) {
      int ret = dfs(next);
      if (ret == LOOPED)
        return ret;
      len[i] = max(len[i], ret + 1);
    }
  }

  return len[i];
}

int main(void) {
  IOS int n;
  cin >> n;
  vector<vector<int>> v;

  REP(i, n) {
    vector<int> v2(n - 1);
    REP(j, n - 1) {
      cin >> v2[j];
      v2[j]--;
    }
    v.emplace_back(v2);
  }

  int m_max = 0;
  REP(i, n - 1) FOR(j, i + 1, n) {
    m_no[i][j] = m_max;
    m_no[j][i] = m_max;
    m_max++;
  }

  REP(i, n) REP(j, n - 1) {
    m_g[i][j] = m_no[i][v[i][j]];
    if (j != 0)
      to[m_g[i][j - 1]].emplace_back(m_g[i][j]);
  }

  int ans = 0;
  REP(i, n) {
    ans = max(ans, dfs(m_g[i][0]));
    if (ans == LOOPED) {
      ans = -1;
      break;
    }
  }

  cout << ans << endl;

  return 0;
}