Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 1010;
const int MAXM = 200010;
const int MAXK = 110;
const int MAXQ = 200010;

int n, pos[MAXN];
vector<int> a[MAXN], tmp;
set<II> s;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  FOR(i, 1, n)
  FOR(j, 1, n - 1) {
    int u;
    cin >> u;
    a[i].emplace_back(u);
  }
  FOR(i, 1, n) pos[i] = 0;
  FOR(i, 1, n) {
    int g = a[i][pos[i]];
    int h = a[g][pos[g]];
    if (h == i) {
      if (i < g)
        s.insert(II(i, g));
      else
        s.insert(II(g, i));
    }
  }
  // FORALL(it, s) cout << it->FI << ' ' << it->SE << endl;
  int cnt = 0;
  while (SZ(s)) {
    cnt++;
    FORALL(it, s) {
      int g = it->FI;
      int h = it->SE;
      pos[g]++;
      pos[h]++;
      tmp.emplace_back(g);
      tmp.emplace_back(h);
    }
    // FOR(i,1,n) cout << pos[i] << ' '; cout << endl;
    s.clear();
    FOR(i, 0, SZ(tmp) - 1) {
      int p = tmp[i];
      if (pos[p] >= n - 1)
        continue;
      int g = a[p][pos[p]];
      if (pos[g] >= n - 1)
        continue;
      int h = a[g][pos[g]];
      if (h == p) {
        if (p < g)
          s.insert(II(p, g));
        else
          s.insert(II(g, p));
      }
    }
  }
  FOR(i, 1, n)
  if (pos[i] < n - 1) {
    cout << -1;
    return 0;
  }
  cout << cnt;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 1010;
const int MAXM = 200010;
const int MAXK = 110;
const int MAXQ = 200010;

int n, pos[MAXN];
vector<int> a[MAXN], tmp;
set<II> s;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  FOR(i, 1, n)
  FOR(j, 1, n - 1) {
    int u;
    cin >> u;
    a[i].emplace_back(u);
  }
  FOR(i, 1, n) pos[i] = 0;
  FOR(i, 1, n) {
    int g = a[i][pos[i]];
    int h = a[g][pos[g]];
    if (h == i) {
      if (i < g)
        s.insert(II(i, g));
      else
        s.insert(II(g, i));
    }
  }
  // FORALL(it, s) cout << it->FI << ' ' << it->SE << endl;
  int cnt = 0;
  while (SZ(s)) {
    cnt++;
    FORALL(it, s) {
      int g = it->FI;
      int h = it->SE;
      pos[g]++;
      pos[h]++;
      tmp.emplace_back(g);
      tmp.emplace_back(h);
    }
    // FOR(i,1,n) cout << pos[i] << ' '; cout << endl;
    s.clear();
    FOR(i, 0, SZ(tmp) - 1) {
      int p = tmp[i];
      if (pos[p] >= n - 1)
        continue;
      int g = a[p][pos[p]];
      if (pos[g] >= n - 1)
        continue;
      int h = a[g][pos[g]];
      if (h == p) {
        if (p < g)
          s.insert(II(p, g));
        else
          s.insert(II(g, p));
      }
    }
    tmp.clear();
  }
  FOR(i, 1, n)
  if (pos[i] < n - 1) {
    cout << -1;
    return 0;
  }
  cout << cnt;
  return 0;
}