Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lson rt << 1
#define rson rt << 1 | 1
#define scd(x) scanf("%d", &x)
#define scdd(x, y) scanf("%d%d", &x, &y)
const int maxn = 1000 + 10;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
map<pii, int> mp;
int arr[maxn][maxn];
int in[maxn * maxn];
vector<int> G[maxn];
pii get_pii(int i, int j) {
  int mn = min(i, arr[i][j]), mx = max(i, arr[i][j]);
  return pii(mn, mx);
}
int topo(int n) {
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0)
      q.push(i);
  }
  int ans = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    ans++;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      in[v]--;
      if (in[v] == 0)
        q.push(v);
    }
  }
  return ans;
}
int dp[maxn * maxn];
int dfs(int u) {
  if (~dp[u])
    return dp[u];
  int mx = 0;
  for (int i = 0; i < G[u].size(); i++) {
    mx = max(mx, dfs(G[u][i]));
  }
  return dp[u] = mx + 1;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif // LOCAL
  int n;
  scd(n);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; j++) {
      scd(arr[i][j]);
      pii no = get_pii(i, j);
      if (mp[no] == 0) {
        mp[no] = ++cnt;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 2; j++) {
      pii one = get_pii(i, j), two = get_pii(i, j + 1);
      int on = mp[one], tw = mp[two];
      G[on].push_back(tw);
      in[tw]++;
    }
  }
  int ans = topo(cnt);
  if (ans == cnt) {
    memset(dp, -1, sizeof(dp));
    ans = 0;
    for (int i = 1; i <= cnt; i++) {
      ans = max(ans, dfs(i));
    }
    cout << ans << endl;
  } else
    cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lson rt << 1
#define rson rt << 1 | 1
#define scd(x) scanf("%d", &x)
#define scdd(x, y) scanf("%d%d", &x, &y)
const int maxn = 1000 + 10;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
map<pii, int> mp;
int arr[maxn][maxn], in[maxn * maxn];
vector<int> G[maxn * maxn];
pii get_pii(int i, int j) {
  int mn = min(i, arr[i][j]), mx = max(i, arr[i][j]);
  return pii(mn, mx);
}
int topo(int n) {
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0)
      q.push(i);
  }
  int ans = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    ans++;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      in[v]--;
      if (in[v] == 0)
        q.push(v);
    }
  }
  return ans;
}
int dp[maxn * maxn];
int dfs(int u) {
  if (~dp[u])
    return dp[u];
  int mx = 0;
  for (int i = 0; i < G[u].size(); i++) {
    mx = max(mx, dfs(G[u][i]));
  }
  return dp[u] = mx + 1;
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif // LOCAL
  int n;
  scd(n);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; j++) {
      scd(arr[i][j]);
      pii no = get_pii(i, j);
      if (mp[no] == 0) {
        mp[no] = ++cnt;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 2; j++) {
      pii one = get_pii(i, j), two = get_pii(i, j + 1);
      int on = mp[one], tw = mp[two];
      G[on].push_back(tw);
      in[tw]++;
    }
  }
  int ans = topo(cnt);
  if (ans == cnt) {
    memset(dp, -1, sizeof(dp));
    ans = 0;
    for (int i = 1; i <= cnt; i++) {
      ans = max(ans, dfs(i));
    }
    cout << ans << endl;
  } else
    cout << -1 << endl;
}