Problem p02925 (C++)

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

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// chmax, chmin
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[1000010];
int vis[1000010];

vector<int> es[1000010];

int dfs(int x) {
  if (vis[x]) {
    cout << -1 << endl;
    exit(0);
  }
  if (x == 1000000)
    return 0;
  if (dp[x] != -1)
    return dp[x];

  vis[x] = true;

  int ans = -1e9;
  for (auto i : es[x]) {
    ans = max(ans, dfs(i) + 1);
  }
  vis[x] = false;
  return ans;
}

void solve(long long N, std::vector<std::deque<long long>> A) {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      A[i][j]--;
      int I = A[i][j];
      int J = i;
      if (I > J)
        swap(I, J);
      A[i][j] = I * 1000 + J;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 2; j++) {
      es[A[i][j]].push_back(A[i][j + 1]);
    }
    es[A[i].back()].push_back(1000000);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dfs(A[i][0]));
  }

  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<std::deque<long long>> A(N, std::deque<long long>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      scanf("%lld", &A[i][j]);
    }
  }
  solve(N, std::move(A));
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// chmax, chmin
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dp[1000010];
int vis[1000010];

vector<int> es[1000010];

int dfs(int x) {
  if (vis[x]) {
    cout << -1 << endl;
    exit(0);
  }
  if (x == 1000000)
    return 0;
  if (dp[x] != -1)
    return dp[x];

  vis[x] = true;

  int ans = -1e9;
  for (auto i : es[x]) {
    ans = max(ans, dfs(i) + 1);
  }
  vis[x] = false;
  return dp[x] = ans;
}

void solve(long long N, std::vector<std::deque<long long>> A) {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      A[i][j]--;
      int I = A[i][j];
      int J = i;
      if (I > J)
        swap(I, J);
      A[i][j] = I * 1000 + J;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 2; j++) {
      es[A[i][j]].push_back(A[i][j + 1]);
    }
    es[A[i].back()].push_back(1000000);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dfs(A[i][0]));
  }

  cout << ans << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<std::deque<long long>> A(N, std::deque<long long>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      scanf("%lld", &A[i][j]);
    }
  }
  solve(N, std::move(A));
  return 0;
}