Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#ifdef TEST
#include <mytest.h>
#else
#define open(...) 0
#define print(...) 42
#endif
using namespace std;
namespace io {
const int SZ = (1 << 22) + 1;
char buf[SZ], *ptr = NULL, *bnd = NULL;
#define GC()                                                                   \
  ((ptr == bnd) ? (ptr = buf, bnd = buf + fread(buf, 1, SZ, stdin),            \
                   (ptr == bnd) ? EOF : (*(ptr++)))                            \
                : (*(ptr++)))
#define STATE(c)                                                               \
  {                                                                            \
    if (c == '-')                                                              \
      sgn = -1;                                                                \
    else if (c == EOF)                                                         \
      return false;                                                            \
  }
inline bool skip(const char &c) { return c < '0' || c > '9'; }
template <class V> inline bool Read(V &v) {
  register char c, sgn = 1;
  while (skip(c = GC()))
    STATE(c);
  for (v = c - '0'; !skip(c = GC()); v = v * 10 + c - '0')
    ;
  return (v *= sgn), true;
}
char oBuf[SZ], *oCur = oBuf, *oBnd = oBuf + SZ, oStk[21], top = 0;
inline void flush() {
  if (oCur - oBuf)
    fwrite(oBuf, 1, oCur - oBuf, stdout), oCur = oBuf;
}
inline void pc(char c) {
  *(oCur++) = c;
  if (oCur == oBnd)
    flush();
}
template <class V> inline void Print(V v) {
  if (!v)
    return pc('0');
  if (v < 0)
    v = -v, pc('-');
  while (v)
    oStk[top++] = v % 10, v /= 10;
  while (top)
    pc(oStk[--top] + '0');
}
template <class V> inline void Println(const V &v) { Print(v), pc('\n'); }
struct flusher {
  ~flusher() { flush(); }
} __flusher__;
} // namespace io
using io::Println;
using io::Read;
const int N = 1024;
int a[N][N];
bool vis[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  // cin >> n;
  Read(n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      // cin >> a[i][j];
      Read(a[i][j]);
    }
    a[i][0] = 1;
  }

  int ans = 0, flg = 1, cnt = 0;
  while (flg) {
    flg = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (a[i][0] == n || vis[i])
        continue;
      int t = a[i][a[i][0]];
      if (i == a[t][a[t][0]] && !vis[t]) {
        flg = 1;
        vis[i] = vis[t] = 1, cnt++;
        a[t][0]++, a[i][0]++;
      }
    }
    if (flg)
      ans++;
  }
  if (cnt != n * (n - 1) / 2)
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#ifdef TEST
#include <mytest.h>
#else
#define open(...) 0
#define print(...) 42
#endif
using namespace std;
namespace io {
const int SZ = (1 << 22) + 1;
char buf[SZ], *ptr = NULL, *bnd = NULL;
#define GC()                                                                   \
  ((ptr == bnd) ? (ptr = buf, bnd = buf + fread(buf, 1, SZ, stdin),            \
                   (ptr == bnd) ? EOF : (*(ptr++)))                            \
                : (*(ptr++)))
#define STATE(c)                                                               \
  {                                                                            \
    if (c == '-')                                                              \
      sgn = -1;                                                                \
    else if (c == EOF)                                                         \
      return false;                                                            \
  }
inline bool skip(const char &c) { return c < '0' || c > '9'; }
template <class V> inline bool Read(V &v) {
  register char c, sgn = 1;
  while (skip(c = GC()))
    STATE(c);
  for (v = c - '0'; !skip(c = GC()); v = v * 10 + c - '0')
    ;
  return (v *= sgn), true;
}
char oBuf[SZ], *oCur = oBuf, *oBnd = oBuf + SZ, oStk[21], top = 0;
inline void flush() {
  if (oCur - oBuf)
    fwrite(oBuf, 1, oCur - oBuf, stdout), oCur = oBuf;
}
inline void pc(char c) {
  *(oCur++) = c;
  if (oCur == oBnd)
    flush();
}
template <class V> inline void Print(V v) {
  if (!v)
    return pc('0');
  if (v < 0)
    v = -v, pc('-');
  while (v)
    oStk[top++] = v % 10, v /= 10;
  while (top)
    pc(oStk[--top] + '0');
}
template <class V> inline void Println(const V &v) { Print(v), pc('\n'); }
struct flusher {
  ~flusher() { flush(); }
} __flusher__;
} // namespace io
using io::Println;
using io::Read;
const int N = 1005;
int a[N][N];
bool vis[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  // cin >> n;
  Read(n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      // cin >> a[i][j];
      Read(a[i][j]);
    }
    a[i][0] = 1;
  }

  int ans = 0, flg = 1, cnt = 0;
  while (flg) {
    flg = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (a[i][0] == n || vis[i])
        continue;
      int t = a[i][a[i][0]];
      if (i == a[t][a[t][0]] && !vis[t]) {
        flg = 1;
        vis[i] = vis[t] = 1, cnt++;
        a[t][0]++, a[i][0]++;
      }
    }
    if (flg)
      ans++;
  }
  if (cnt != n * (n - 1) / 2)
    ans = -1;
  cout << ans << endl;
}