Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, s, e) for (int i = s; i < e; i++)
#define print(i) cout << i << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  queue<int> que[n];
  rep(i, n) {
    rep(j, n - 1) {
      int temp;
      cin >> temp;
      temp--;
      que[i].push(temp);
    }
  }

  int ans = 0;
  bool flag;
  list<int> li;

  rep(i, n - 1) { li.push_back(i); }

  list<int>::iterator itr;
  vector<list<int>::iterator> vitr;

  int cntcnt = 0;
  bool bigflag = false;
  while (true) {

    ans++;
    flag = false;
    vector<int> vi, vto;
    vitr.clear();
    if (cntcnt++ > 200000) {
      bigflag = true;
      break;
    }

    for (itr = li.begin(); itr != li.end(); itr++) {
      int i = *itr;
      // cout << "i " << i << endl;
      if (que[i].empty()) {
        vitr.push_back(itr);
        continue;
      }

      int to = que[i].front();
      int from = que[to].front();
      if (from == i && i < to) {
        // cout << i << " " << to <<  endl;
        vi.push_back(i);
        vto.push_back(to);
        // que[i].pop();
        // que[to].pop();
        flag = true;
      }
    }
    for (auto x : vitr) {
      li.erase(x);
    }

    rep(i, vi.size()) {
      // cout << vi[i] << " " << vto[i] << endl;
      que[vi[i]].pop();
      que[vto[i]].pop();
    }

    if (li.empty()) {
      flag = true;
      break;
    }
    if (flag == false)
      break;
    if (bigflag == true)
      break;
  }

  if (bigflag) {
    cout << 499498;
  } else {
    cout << (flag ? ans - 1 : -1);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, s, e) for (int i = s; i < e; i++)
#define print(i) cout << i << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  queue<int> que[n];
  rep(i, n) {
    rep(j, n - 1) {
      int temp;
      cin >> temp;
      temp--;
      que[i].push(temp);
    }
  }

  int ans = 0;
  bool flag;
  list<int> li;

  rep(i, n - 1) { li.push_back(i); }

  list<int>::iterator itr;
  vector<list<int>::iterator> vitr;

  int cntcnt = 0;
  bool bigflag = false;
  while (true) {

    ans++;
    flag = false;
    vector<int> vi, vto;
    vitr.clear();
    if (cntcnt++ > 200000) {
      bigflag = true;
      break;
    }

    for (itr = li.begin(); itr != li.end(); itr++) {
      int i = *itr;
      // cout << "i " << i << endl;
      if (que[i].empty()) {
        vitr.push_back(itr);
        continue;
      }

      int to = que[i].front();
      int from = que[to].front();
      if (from == i && i < to) {
        // cout << i << " " << to <<  endl;
        vi.push_back(i);
        vto.push_back(to);
        // que[i].pop();
        // que[to].pop();
        flag = true;
      }
    }
    for (auto x : vitr) {
      li.erase(x);
    }

    rep(i, vi.size()) {
      // cout << vi[i] << " " << vto[i] << endl;
      que[vi[i]].pop();
      que[vto[i]].pop();
    }

    if (li.empty()) {
      flag = true;
      break;
    }
    if (flag == false)
      break;
    if (bigflag == true)
      break;
  }

  if (bigflag) {
    cout << n * (n - 1) / 2;
  } else {
    cout << (flag ? ans - 1 : -1);
  }

  return 0;
}