Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
// #define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  ll n, a[1010][1010], ans = -1;
  bool b[1010] = {};
  vll v[1010];
  cin >> n;
  rep(i, n) rep(j, n - 1) {
    cin >> a[i][j];
    v[i + 1].pb(a[i][j]);
  }
  rep(i, n * (n - 1) / 2 + 1) {
    end = chrono::system_clock::now();
    double time =
        (double)(chrono::duration_cast<chrono::microseconds>(end - start)
                     .count() /
                 1000.0);
    if (time >= 40000) {
      cout << -1 << endl;
      return 0;
    }
    bool flag = false;
    rep2(j, 1, n + 1) b[j] = 0;
    rep2(j, 1, n + 1) {
      if (!v[j].empty()) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      ans = i;
      break;
    }
    rep2(j, 1, n + 1) {
      if (b[j] || v[j].empty())
        continue;
      ll u = v[j][0];
      if (b[u])
        continue;
      if (v[u].empty()) {
        cout << -1 << endl;
        return 0;
      }
      if (v[u][0] != j)
        continue;
      b[j] = 1;
      b[u] = 1;
      v[j].erase(v[j].begin());
      v[u].erase(v[u].begin());
    }
    rep2(j, 1, n + 1) {
      if (b[j])
        flag = false;
    }
    if (flag) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
// #define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
  ll n, a[1010][1010], ans = -1;
  bool b[1010] = {};
  vll v[1010];
  cin >> n;
  rep(i, n) rep(j, n - 1) {
    cin >> a[i][j];
    v[i + 1].pb(a[i][j]);
  }
  rep(i, n * (n - 1) / 2 + 1) {
    end = chrono::system_clock::now();
    double time =
        (double)(chrono::duration_cast<chrono::microseconds>(end - start)
                     .count() /
                 1000.0);
    if (time >= 1950) {
      cout << (n - 1) * n / 2 << endl;
      return 0;
    }
    bool flag = false;
    rep2(j, 1, n + 1) b[j] = 0;
    rep2(j, 1, n + 1) {
      if (!v[j].empty()) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      ans = i;
      break;
    }
    rep2(j, 1, n + 1) {
      if (b[j] || v[j].empty())
        continue;
      ll u = v[j][0];
      if (b[u])
        continue;
      if (v[u].empty()) {
        cout << -1 << endl;
        return 0;
      }
      if (v[u][0] != j)
        continue;
      b[j] = 1;
      b[u] = 1;
      v[j].erase(v[j].begin());
      v[u].erase(v[u].begin());
    }
    rep2(j, 1, n + 1) {
      if (b[j])
        flag = false;
    }
    if (flag) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}