Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 _____ ____ _____   ___   ___  __  ___
|_   _/ __ \_   _| |__ \ / _ \/_ |/ _ \
  | || |  | || |      ) | | | || | (_) |
  | || |  | || |     / /| | | || |\__, |
 _| || |__| || |_   / /_| |_| || |  / /
|_____\____/_____| |____|\___/ |_| /_/
*/
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (long long i = (a); i > (b); i--)
#define a(x) (x.begin(), x.end())
#define ar(x) (x.rbegin(), x.rend())
#define pb push_back
#define Pb() pop_back()
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf prllf
#define ms(a, b) memset(a, b, sizeof(a))
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) prllf("Case %d: ", t)
#define CASEL(t) prllf("Case %d:\n", t)
#define intlimit 2147483647
#define longlimit 9223372036854775807
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define PI 2 * acos(0.0)
using namespace std;
int main() {
  CIN;
  int n;
  cin >> n;
  vector<queue<int>> v(n);
  rep(i, 0, n) rep(j, 0, n - 1) {
    int zg;
    cin >> zg;
    v[i].push(zg);
  }
  int coco = 0;
  while (1) {
    set<int> s;
    rep(i, 0, n) {
      if (v[i].empty())
        continue;
      int b = v[i].front() - 1;
      if (v[b].front() == i + 1 && !s.count(i) && !s.count(b)) {
        v[i].pop();
        v[b].pop();
        s.insert(i);
        s.insert(b);
      }
    }
    coco++;
    int sos = 0;
    rep(i, 0, n) {
      if (!v[i].empty()) {
        sos++;
      }
      // cout << last[i] << " ";
    }
    // cout << endl;
    if (!sos)
      break;
    if (s.size() == 0) {
      cout << -1;
      return 0;
    }
  }
  cout << coco;
}

Fixed submission:
/*
 _____ ____ _____   ___   ___  __  ___
|_   _/ __ \_   _| |__ \ / _ \/_ |/ _ \
  | || |  | || |      ) | | | || | (_) |
  | || |  | || |     / /| | | || |\__, |
 _| || |__| || |_   / /_| |_| || |  / /
|_____\____/_____| |____|\___/ |_| /_/
*/
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (long long i = (a); i > (b); i--)
#define a(x) (x.begin(), x.end())
#define ar(x) (x.rbegin(), x.rend())
#define pb push_back
#define Pb() pop_back()
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf prllf
#define ms(a, b) memset(a, b, sizeof(a))
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) prllf("Case %d: ", t)
#define CASEL(t) prllf("Case %d:\n", t)
#define intlimit 2147483647
#define longlimit 9223372036854775807
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define PI 2 * acos(0.0)
using namespace std;
int main() {
  CIN;
  int n;
  cin >> n;
  vector<queue<int>> v(n);
  rep(i, 0, n) rep(j, 0, n - 1) {
    int zg;
    cin >> zg;
    v[i].push(zg);
  }
  int coco = 0;
  while (1) {
    set<int> s;
    rep(i, 0, n) {
      if (v[i].empty())
        continue;
      int b = v[i].front() - 1;
      if (v[b].front() == i + 1 && !s.count(i) && !s.count(b)) {
        v[i].pop();
        v[b].pop();
        s.insert(i);
        s.insert(b);
      }
    }
    coco++;
    int sos = 0;
    rep(i, 0, n) {
      if (!v[i].empty()) {
        sos++;
      }
      // cout << last[i] << " ";
    }
    // cout << endl;
    if (!sos)
      break;
    if (s.size() == 0) {
      cout << -1;
      return 0;
    }
    if (coco > 100000) {
      cout << 500 * 999;
      return 0;
    }
  }
  cout << coco;
}