Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

typedef vector<ll> Edges;
typedef vector<Edges> Graph;

const ll V_MAX = 101010;
vector<ll> ts(V_MAX, -1);
int topologicalSort(Graph &G) {
  ll V = G.size();
  vector<ll> indeg(V, 0);
  queue<ll> qu;

  REP(i, V) {
    REP(j, G[i].size()) { indeg[G[i][j]]++; }
  }
  REP(i, V) {
    if (!indeg[i])
      qu.push(i);
  }

  ll depth = 0;
  while (!qu.empty()) {
    ll s = qu.size();
    REP(i, s) {
      ll now = qu.front();
      qu.pop();
      ts[now] = depth;
      REP(j, G[now].size()) {
        ll next = G[now][j];
        indeg[next]--;
        if (!indeg[next])
          qu.push(next);
      }
    }
    depth++;
  }

  REP(i, V) {
    if (ts[i] == -1)
      return -1;
  }
  return depth;
}

struct battle {
  ll x, y;
  bool operator<(const battle &right) const {
    return (x != right.x ? x < right.x : y < right.y);
  }
};

vector<battle> id2b(1010101);
map<battle, ll> b2id;
ll V;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;

  ll now = 0;
  REP(i, N) {
    REP(j, N) {
      if (i >= j)
        continue;
      battle b = {i, j};
      id2b[now] = b;
      now++;
    }
  }
  V = N * (N - 1) / 2;
  REP(i, V) { b2id[id2b[i]] = i; }

  Graph G(V);

  REP(i, N) {
    vector<ll> tmp(N - 1);
    REP(j, N - 1) cin >> tmp[j], tmp[j]--;
    REP(j, N - 2) {
      ll x1, y1, x2, y2;
      x1 = i;
      y1 = tmp[j];
      x2 = i;
      y2 = tmp[j + 1];
      if (x1 > y1)
        swap(x1, y1);
      if (x2 > y2)
        swap(x2, y2);
      battle b1 = {x1, y1}, b2 = {x2, y2};
      G[b2id[b1]].push_back(b2id[b2]);
    }
  }

  int res = topologicalSort(G);
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

typedef vector<ll> Edges;
typedef vector<Edges> Graph;

const ll V_MAX = 1010101;
vector<ll> ts(V_MAX, -1);
int topologicalSort(Graph &G) {
  ll V = G.size();
  vector<ll> indeg(V, 0);
  queue<ll> qu;

  REP(i, V) {
    REP(j, G[i].size()) { indeg[G[i][j]]++; }
  }
  REP(i, V) {
    if (!indeg[i])
      qu.push(i);
  }

  ll depth = 0;
  while (!qu.empty()) {
    ll s = qu.size();
    REP(i, s) {
      ll now = qu.front();
      qu.pop();
      ts[now] = depth;
      REP(j, G[now].size()) {
        ll next = G[now][j];
        indeg[next]--;
        if (!indeg[next])
          qu.push(next);
      }
    }
    depth++;
  }

  REP(i, V) {
    if (ts[i] == -1)
      return -1;
  }
  return depth;
}

struct battle {
  ll x, y;
  bool operator<(const battle &right) const {
    return (x != right.x ? x < right.x : y < right.y);
  }
};

vector<battle> id2b(1010101);
map<battle, ll> b2id;
ll V;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;

  ll now = 0;
  REP(i, N) {
    REP(j, N) {
      if (i >= j)
        continue;
      battle b = {i, j};
      id2b[now] = b;
      now++;
    }
  }
  V = N * (N - 1) / 2;
  REP(i, V) { b2id[id2b[i]] = i; }

  Graph G(V);

  REP(i, N) {
    vector<ll> tmp(N - 1);
    REP(j, N - 1) cin >> tmp[j], tmp[j]--;
    REP(j, N - 2) {
      ll x1, y1, x2, y2;
      x1 = i;
      y1 = tmp[j];
      x2 = i;
      y2 = tmp[j + 1];
      if (x1 > y1)
        swap(x1, y1);
      if (x2 > y2)
        swap(x2, y2);
      battle b1 = {x1, y1}, b2 = {x2, y2};
      G[b2id[b1]].push_back(b2id[b2]);
    }
  }

  int res = topologicalSort(G);
  cout << res << endl;
  return 0;
}