Problem p02925 (C++)

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using P = pair<ll, ll>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 29;
const ll INF64 = 1ll << 62;
const int mod = 1000000007;
// const int mod = 998244353;

template <class T> inline bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    // if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint &operator++() {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--() {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i, n) rep(j, n - 1) {
    cin >> a[i][j];
    --a[i][j];
  }
  vector<int> now(n, 0);
  vector<bool> yesterday(n, true);

  for (int day = 0;; ++day) {
    vector<bool> f(n, false);
    bool update = false;
    rep(i, n) {
      if (!yesterday[i])
        continue;
      int j = a[i][now[i]];
      if (f[i] || f[j])
        continue;
      if (a[j][now[j]] != i || now[i] == n - 1) {
        yesterday[i] = false;
        continue;
      }
      update = true;
      ++now[i];
      ++now[j];
      yesterday[i] = true;
      yesterday[j] = true;
      f[i] = true;
      f[j] = true;
    }
    if (!update) {
      rep(i, n) if (now[i] != n - 1) {
        cout << -1 << endl;
        return 0;
      }
      cout << day << endl;
      return 0;
    }
  }
}

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
#define SZ(x) ((int)(x).size())
using ll = long long;
using P = pair<ll, ll>;
using llP = pair<ll, ll>;
using DoP = pair<double, double>;
const int di[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dj[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int INF = 1 << 29;
const ll INF64 = 1ll << 62;
const int mod = 1000000007;
// const int mod = 998244353;

template <class T> inline bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void line_out(const vector<T> vec) {
  int n = SZ(vec);
  rep(i, n) {
    cout << vec[i];
    // if(i < n-1) cout << " ";
  }
  cout << endl;
  return;
}

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint &operator++() {
    if ((x += 1) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator--() {
    if ((x += mod - 1) >= mod)
      x -= mod;
    return *this;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i, n) rep(j, n - 1) {
    cin >> a[i][j];
    --a[i][j];
  }
  vector<int> now(n, 0);
  vector<bool> yesterday(n, true);

  for (int day = 0;; ++day) {
    vector<bool> f(n, false);
    bool update = false;
    rep(i, n) {
      if (!yesterday[i])
        continue;
      int j = a[i][now[i]];
      if (f[i] || f[j])
        continue;
      if (now[i] == n - 1 || a[j][now[j]] != i) {
        yesterday[i] = false;
        continue;
      }
      update = true;
      ++now[i];
      ++now[j];
      yesterday[i] = true;
      yesterday[j] = true;
      f[i] = true;
      f[j] = true;
    }
    if (!update) {
      rep(i, n) if (now[i] != n - 1) {
        cout << -1 << endl;
        return 0;
      }
      cout << day << endl;
      return 0;
    }
  }
}