Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rep1(i, n) for (int i = 1; i <= int(n); i++)
#define repR(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define rep1R(i, n) for (int i = int(n); i >= 1; i--)
#define all(x) (x).begin(), (x).end()
#define show(x) #x " = " << x
#define mp make_pair
#define quit(x)                                                                \
  {                                                                            \
    cout << (x) << endl;                                                       \
    return 0;                                                                  \
  }
template <class T> bool chmin(T &x, T const &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &x, T const &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <class T>
typename conditional<true, ostream &, decltype(declval<T>().begin())>::type
operator<<(ostream &os, T const &v) {
  for (auto const &x : v)
    os << x << " ";
  return os;
}
template <class T>
typename conditional<true, istream &, decltype(declval<T>().begin())>::type
operator>>(istream &is, T &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <class T, class S>
ostream &operator<<(ostream &os, pair<T, S> const &p) {
  return os << "(" << p.fst << ", " << p.snd << ")";
}
template <class T, class S>
istream &operator>>(istream &is, pair<T, S> const &p) {
  return is >> p.fst >> p.snd;
}

int main() {
  int n;
  cin >> n;
  int N = n * (n - 1) / 2;
  vector<vector<int>> g(N);
  vector<int> cnt(N);
  map<pair<int, int>, int> m;
  int c = 0;
  rep(i, n) rep(j, n) if (i < j) m[mp(i, j)] = c++;
  rep(i, n) rep(j, n) if (i > j) m[mp(i, j)] = m[mp(j, i)];

  rep(i, n) {
    vector<int> a(n - 1);
    rep(j, n - 1) cin >> a[j], a[j]--;
    rep(j, n - 2) {
      int x = m[mp(i, a[j])];
      int y = m[mp(i, a[j + 1])];
      g[x].push_back(y);
      cnt[y]++;
    }
  }

  deque<int> q;
  rep(i, N) if (cnt[i] == 0) q.push_back(i);
  vector<int> a;
  while (!q.empty()) {
    int x = q.front();
    q.pop_front();
    a.push_back(x);
    for (int y : g[x])
      if (--cnt[y] == 0)
        q.push_back(y);
  }

  if (a.size() != N)
    quit(-1);

  vector<int> d(N, 1);
  rep(i, N) {
    int x = a[i];
    for (int y : g[x])
      chmax(d[y], d[x] + 1);
  }

  rep(i, N) for (int j : g[i]) if (d[i] >= d[j]) quit(-1);
  cout << *max_element(all(d)) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rep1(i, n) for (int i = 1; i <= int(n); i++)
#define repR(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define rep1R(i, n) for (int i = int(n); i >= 1; i--)
#define all(x) (x).begin(), (x).end()
#define show(x) #x " = " << x
#define mp make_pair
#define quit(x)                                                                \
  {                                                                            \
    cout << (x) << endl;                                                       \
    return 0;                                                                  \
  }
template <class T> bool chmin(T &x, T const &y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &x, T const &y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <class T>
typename conditional<true, ostream &, decltype(declval<T>().begin())>::type
operator<<(ostream &os, T const &v) {
  for (auto const &x : v)
    os << x << " ";
  return os;
}
template <class T>
typename conditional<true, istream &, decltype(declval<T>().begin())>::type
operator>>(istream &is, T &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <class T, class S>
ostream &operator<<(ostream &os, pair<T, S> const &p) {
  return os << "(" << p.fst << ", " << p.snd << ")";
}
template <class T, class S>
istream &operator>>(istream &is, pair<T, S> const &p) {
  return is >> p.fst >> p.snd;
}

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

  int n;
  cin >> n;
  int N = n * (n - 1) / 2;
  vector<vector<int>> g(N);
  vector<int> cnt(N);
  map<pair<int, int>, int> m;
  int c = 0;
  rep(i, n) rep(j, n) if (i < j) m[mp(i, j)] = c++;
  rep(i, n) rep(j, n) if (i > j) m[mp(i, j)] = m[mp(j, i)];

  rep(i, n) {
    vector<int> a(n - 1);
    rep(j, n - 1) cin >> a[j], a[j]--;
    rep(j, n - 2) {
      int x = m[mp(i, a[j])];
      int y = m[mp(i, a[j + 1])];
      g[x].push_back(y);
      cnt[y]++;
    }
  }

  deque<int> q;
  rep(i, N) if (cnt[i] == 0) q.push_back(i);
  vector<int> a;
  while (!q.empty()) {
    int x = q.front();
    q.pop_front();
    a.push_back(x);
    for (int y : g[x])
      if (--cnt[y] == 0)
        q.push_back(y);
  }

  if (a.size() != N)
    quit(-1);

  vector<int> d(N, 1);
  rep(i, N) {
    int x = a[i];
    for (int y : g[x])
      chmax(d[y], d[x] + 1);
  }

  rep(i, N) for (int j : g[i]) if (d[i] >= d[j]) quit(-1);
  cout << *max_element(all(d)) << endl;
}