Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define arep(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> P;

const int MAX_N = 1010;

int A[MAX_N][MAX_N];
int node_number[MAX_N][MAX_N];

vector<int> graph[MAX_N * MAX_N];
int state[MAX_N * MAX_N];

bool dfs(int node, vector<int> &tsorted_node) {
  if (state[node] == GRAY)
    return false;
  bool res = true;
  state[node] = GRAY;
  rep(i, 0, graph[node].size()) { res &= dfs(graph[node][i], tsorted_node); }
  tsorted_node.push_back(node);
  state[node] = BLACK;
  return res;
}

vector<int> tsort(int n) {
  bool is_ok = true;
  vector<int> res;
  rep(i, 0, n) {
    if (state[i] == BLACK)
      continue;
    is_ok &= dfs(i, res);
  }
  if (is_ok) {
    reverse(all(res));
    return res;
  } else
    return vector<int>(0);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  rep(i, 0, N) rep(j, 0, N - 1) {
    cin >> A[i][j];
    A[i][j]--;
  }
  rep(i, 0, N) rep(j, 0, N) node_number[i][j] = -1;
  int number = 0;
  rep(i, 0, N) rep(j, 0, N - 2) {
    if (node_number[i][A[i][j]] < 0)
      node_number[i][A[i][j]] = node_number[A[i][j]][i] = number++;
    if (node_number[i][A[i][j + 1]] < 0)
      node_number[i][A[i][j + 1]] = node_number[A[i][j + 1]][i] = number++;
    graph[node_number[i][A[i][j]]].push_back(node_number[i][A[i][j + 1]]);
  }
  vector<int> nodes = tsort(N * (N - 1) / 2);
  if (nodes.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> dist(nodes.size(), 0);
  rep(i, 0, nodes.size()) {
    rep(j, 0, graph[nodes[i]].size()) {
      dist[graph[nodes[i]][j]] =
          max(dist[graph[nodes[i]][j]], dist[nodes[i]] + 1);
    }
  }
  int ans = 0;
  rep(i, 0, dist.size()) { ans = max(ans, dist[i]); }
  ans++;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= end; --i)
#define arep(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef pair<int, int> P;

const int MAX_N = 1010;

int A[MAX_N][MAX_N];
int node_number[MAX_N][MAX_N];

vector<int> graph[MAX_N * MAX_N];
int state[MAX_N * MAX_N];

bool dfs(int node, vector<int> &tsorted_node) {
  if (state[node] == GRAY)
    return false;
  if (state[node] == BLACK)
    return true;
  bool res = true;
  state[node] = GRAY;
  rep(i, 0, graph[node].size()) { res &= dfs(graph[node][i], tsorted_node); }
  tsorted_node.push_back(node);
  state[node] = BLACK;
  return res;
}

vector<int> tsort(int n) {
  bool is_ok = true;
  vector<int> res;
  rep(i, 0, n) {
    if (state[i] == BLACK)
      continue;
    is_ok &= dfs(i, res);
  }
  if (is_ok) {
    reverse(all(res));
    return res;
  } else
    return vector<int>(0);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  rep(i, 0, N) rep(j, 0, N - 1) {
    cin >> A[i][j];
    A[i][j]--;
  }
  rep(i, 0, N) rep(j, 0, N) node_number[i][j] = -1;
  int number = 0;
  rep(i, 0, N) rep(j, 0, N - 2) {
    if (node_number[i][A[i][j]] < 0)
      node_number[i][A[i][j]] = node_number[A[i][j]][i] = number++;
    if (node_number[i][A[i][j + 1]] < 0)
      node_number[i][A[i][j + 1]] = node_number[A[i][j + 1]][i] = number++;
    graph[node_number[i][A[i][j]]].push_back(node_number[i][A[i][j + 1]]);
  }
  vector<int> nodes = tsort(N * (N - 1) / 2);
  if (nodes.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> dist(nodes.size(), 0);
  rep(i, 0, nodes.size()) {
    rep(j, 0, graph[nodes[i]].size()) {
      dist[graph[nodes[i]][j]] =
          max(dist[graph[nodes[i]][j]], dist[nodes[i]] + 1);
    }
  }
  int ans = 0;
  rep(i, 0, dist.size()) { ans = max(ans, dist[i]); }
  ans++;
  cout << ans << endl;
  return 0;
}