Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vvi a(n, vi(n - 1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  vi idx(n, 0);
  // vi to_ch(n, 0);
  // iota(to_ch.begin(), to_ch.end(), 0);
  set<int> to_check;
  for (int i = 0; i < n; ++i) {
    to_check.insert(i);
  }
  int days = 0;
  int used = 0;
  while (used < n * (n - 1)) {
    days++;
    set<int> temp;
    for (auto &aidx : to_check) {
      int tag = a[aidx][idx[aidx]];
      if (a[tag][idx[tag]] == aidx && temp.count(aidx) == 0) {
        // idx[aidx]++;
        // idx[tag]++;
        temp.insert(aidx);
        temp.insert(tag);
        used += 2;
      }
    }
    if (temp.size() == 0) {
      cout << -1 << "\n";
      return 0;
    } else {
      for (auto &t : temp) {
        idx[t]++;
      }
      to_check = temp;
    }
  }
  cout << days << "\n";
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vvi a(n, vi(n - 1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  vi idx(n, 0);
  // vi to_ch(n, 0);
  // iota(to_ch.begin(), to_ch.end(), 0);
  set<int> to_check;
  for (int i = 0; i < n; ++i) {
    to_check.insert(i);
  }
  int days = 0;
  int used = 0;
  while (used < n * (n - 1)) {
    days++;
    set<int> temp;
    for (auto &aidx : to_check) {
      if (idx[aidx] >= n - 1 || idx[a[aidx][idx[aidx]]] >= n - 1)
        continue;
      int tag = a[aidx][idx[aidx]];
      if (a[tag][idx[tag]] == aidx && temp.count(aidx) == 0) {
        // idx[aidx]++;
        // idx[tag]++;
        temp.insert(aidx);
        temp.insert(tag);
        used += 2;
      }
    }
    if (temp.size() == 0) {
      cout << -1 << "\n";
      return 0;
    } else {
      for (auto &t : temp) {
        idx[t]++;
      }
      to_check = temp;
    }
  }
  cout << days << "\n";
  return 0;
}