Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int n;
int a[1000][1000];
vector<int> e[1000000];
bool saw[1000000];
int cost[1000000];

bool dfs(int v) {
  saw[v] = true;
  int cnt = 0;
  rep(i, e[v].size()) {
    if (saw[e[v][i]] && cost[e[v][i]] == -1) {
      return false;
    }
    if (saw[e[v][i]]) {
      cnt = max(cnt, cost[e[v][i]]);
    }
    if (!dfs(e[v][i]))
      return false;
    cnt = max(cnt, cost[e[v][i]]);
  }
  cost[v] = cnt + 1;
  return true;
}

int main() {
  memset(cost, -1, sizeof(cost));
  cin >> n;
  rep(i, n) {
    rep(j, n - 1) cin >> a[i][j];
    rep(j, n - 1) a[i][j]--;
    for (int j = 1; j < n - 1; j++) {
      pi x = pi(i, a[i][j - 1]);
      pi y = pi(i, a[i][j]);
      if (x.second < x.first)
        swap(x.first, x.second);
      if (y.second < y.first)
        swap(y.first, y.second);
      e[x.first * 1000 + x.second].push_back(y.first * 1000 + y.second);
    }
  }
  rep(j, n) {
    for (int k = j + 1; k < n; k++) {
      int id = j * 1000 + k;
      if (saw[id])
        continue;
      if (!dfs(id)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int ans = 0;
  rep(j, n) {
    for (int k = j + 1; k < n; k++) {
      int id = j * 1000 + k;
      ans = max(ans, cost[id]);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.1415926535897932;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int n;
int a[1000][1000];
vector<int> e[1000000];
bool saw[1000000];
int cost[1000000];

bool dfs(int v) {
  saw[v] = true;
  int cnt = 0;
  rep(i, e[v].size()) {
    if (saw[e[v][i]] && cost[e[v][i]] == -1) {
      return false;
    }
    if (saw[e[v][i]]) {
      cnt = max(cnt, cost[e[v][i]]);
      continue;
    }
    if (!dfs(e[v][i]))
      return false;
    cnt = max(cnt, cost[e[v][i]]);
  }
  cost[v] = cnt + 1;
  return true;
}

int main() {
  memset(cost, -1, sizeof(cost));
  cin >> n;
  rep(i, n) {
    rep(j, n - 1) cin >> a[i][j];
    rep(j, n - 1) a[i][j]--;
    for (int j = 1; j < n - 1; j++) {
      pi x = pi(i, a[i][j - 1]);
      pi y = pi(i, a[i][j]);
      if (x.second < x.first)
        swap(x.first, x.second);
      if (y.second < y.first)
        swap(y.first, y.second);
      e[x.first * 1000 + x.second].push_back(y.first * 1000 + y.second);
    }
  }
  rep(j, n) {
    for (int k = j + 1; k < n; k++) {
      int id = j * 1000 + k;
      if (saw[id])
        continue;
      if (!dfs(id)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int ans = 0;
  rep(j, n) {
    for (int k = j + 1; k < n; k++) {
      int id = j * 1000 + k;
      ans = max(ans, cost[id]);
    }
  }
  cout << ans << endl;
}