Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>;
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
map<pair<int, int>, int> mp;
int cnt = 1;
int head[maxn];
int degree[maxn];
int visited[maxn];
struct node {
  int u, v, next;
} e[maxn];
void addedge(int u, int v) {
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt++;
}
int main() {
  int n;
  scanf("%d", &n);
  int cnt1 = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      mp[make_pair(i, j)] = cnt1++;
    }
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    int flag = mp[make_pair(min(i, x), max(i, x))];
    for (int j = 2; j <= n - 1; j++) {
      int y;
      scanf("%d", &y);
      addedge(flag, mp[make_pair(min(i, y), max(i, y))]);
      degree[mp[make_pair(min(i, y), max(i, y))]]++;
      flag = mp[make_pair(min(i, y), max(i, y))];
    }
  }
  queue<int> q;
  for (int i = 1; i <= n * (n - 1) / 2; i++) {
    if (degree[i] == 0) {
      q.push(i);
    }
  }
  int ans = 0;
  while (!q.empty()) {
    int sz = q.size();
    for (int i = 1; i <= sz; i++) {
      int x = q.front();
      for (int j = head[x]; j != 0; j = e[j].next) {
        int y = e[j].v;
        degree[y]--;
        if (degree[y] == 0) {
          q.push(y);
        }
      }
      visited[x] = 1;
      q.pop();
    }
    ans++;
  }
  int flag = 1;
  for (int i = 1; i <= n * (n - 1) / 2; i++) {
    if (visited[i] == 0) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    printf("%d\n", ans);
  } else
    printf("-1\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>;
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
map<pair<int, int>, int> mp;
int cnt = 1;
int head[maxn];
int degree[maxn];
int visited[maxn];
struct node {
  int u, v, next;
} e[maxn];
void addedge(int u, int v) {
  e[cnt].u = u;
  e[cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt++;
}
int main() {
  int n;
  scanf("%d", &n);
  int cnt1 = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      mp[make_pair(i, j)] = cnt1++;
    }
  }
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    int flag = mp[make_pair(min(i, x), max(i, x))];
    for (int j = 2; j <= n - 1; j++) {
      int y;
      scanf("%d", &y);
      addedge(flag, mp[make_pair(min(i, y), max(i, y))]);
      degree[mp[make_pair(min(i, y), max(i, y))]]++;
      flag = mp[make_pair(min(i, y), max(i, y))];
    }
  }
  queue<int> q;
  for (int i = 1; i <= n * (n - 1) / 2; i++) {
    if (degree[i] == 0) {
      q.push(i);
    }
  }
  int ans = 0;
  while (!q.empty()) {
    int sz = q.size();
    for (int i = 1; i <= sz; i++) {
      int x = q.front();
      for (int j = head[x]; j != 0; j = e[j].next) {
        int y = e[j].v;
        degree[y]--;
        if (degree[y] == 0) {
          q.push(y);
        }
      }
      visited[x] = 1;
      q.pop();
    }
    ans++;
  }
  int flag = 1;
  for (int i = 1; i <= n * (n - 1) / 2; i++) {
    if (visited[i] == 0) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    printf("%d\n", ans);
  } else
    printf("-1\n");
  return 0;
}