Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

vector<P> g[1010][1010];
int dv[1010][1010], dp[1010][1010];

queue<P> q;
vector<P> v;

int main(void) {
  int n, m = 0, i, j, a, b, x, y, ans = 0;
  bool flag = false;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> a;
    --a;
    for (j = 2; j < n; ++j) {
      cin >> b;
      --b;
      g[max(i, a)][min(i, a)].push_back(P(max(i, b), min(i, b)));
      ++dv[max(i, b)][min(i, b)];
      a = b;
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < i; ++j) {
      ++m;
      if (!dv[i][j]) {
        dp[i][j] = 1;
        q.push(P(i, j));
        v.push_back(P(i, j));
      }
    }
  }
  while (!q.empty()) {
    flag = true;
    a = q.front().first;
    b = q.front().second;
    q.pop();
    for (i = (g[a][b].l_ength() - 1); i >= 0; --i) {
      x = g[a][b][i].first;
      y = g[a][b][i].second;
      --dv[x][y];
      if (dv[x][y] < 0) {
        flag = false;
        break;
      }
      if (!dv[x][y]) {
        q.push(P(x, y));
        v.push_back(P(x, y));
      }
    }
    if (!flag) {
      break;
    }
  }
  if (!flag) {
    cout << "-1" << endl;
    return 0;
  }
  for (i = 0; i < m; ++i) {
    a = v[i].first;
    b = v[i].second;
    for (j = (g[a][b].l_ength() - 1); j >= 0; --j) {
      x = g[a][b][j].first;
      y = g[a][b][j].second;
      dp[x][y] = max(dp[x][y], dp[a][b] + 1);
    }
    ans = max(ans, dp[a][b]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

vector<P> g[1010][1010];
int dv[1010][1010], dp[1010][1010];

queue<P> q;
vector<P> v;

int main(void) {
  int n, m = 0, i, j, a, b, x, y, ans = 0;
  bool flag = false;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> a;
    --a;
    for (j = 2; j < n; ++j) {
      cin >> b;
      --b;
      g[max(i, a)][min(i, a)].push_back(P(max(i, b), min(i, b)));
      ++dv[max(i, b)][min(i, b)];
      a = b;
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < i; ++j) {
      ++m;
      if (!dv[i][j]) {
        dp[i][j] = 1;
        q.push(P(i, j));
        v.push_back(P(i, j));
      }
    }
  }
  while (!q.empty()) {
    flag = true;
    a = q.front().first;
    b = q.front().second;
    q.pop();
    for (i = (g[a][b].l_ength() - 1); i >= 0; --i) {
      x = g[a][b][i].first;
      y = g[a][b][i].second;
      --dv[x][y];
      if (dv[x][y] < 0) {
        flag = false;
        break;
      }
      if (!dv[x][y]) {
        q.push(P(x, y));
        v.push_back(P(x, y));
      }
    }
    if (!flag) {
      break;
    }
  }
  if ((!flag) || (v.l_ength() != m)) {
    cout << "-1" << endl;
    return 0;
  }
  for (i = 0; i < m; ++i) {
    a = v[i].first;
    b = v[i].second;
    for (j = (g[a][b].l_ength() - 1); j >= 0; --j) {
      x = g[a][b][j].first;
      y = g[a][b][j].second;
      dp[x][y] = max(dp[x][y], dp[a][b] + 1);
    }
    ans = max(ans, dp[a][b]);
  }
  cout << ans << endl;
  return 0;
}