Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define out(str) cout << str << endl
#define ALL(a) (a).begin(), (a).end()
#define INF (1 << 29)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N, A, cnt, now, sum = 0;
bool flag = true, te[1000] = {};

int main() {
  cin >> N;
  queue<int> opp[N];
  rep(i, N) {
    rep(j, N - 1) {
      cin >> A;
      A--;
      opp[i].push(A);
    }
  }
  while (flag) {
    // out(cnt);
    flag = false;
    bool lo[N];
    // rep(i,N){
    //     lo[i]=false;
    // }
    memcpy(lo, te, sizeof(lo));
    rep(i, N) {
      if (lo[i] || opp[i].size() == 0)
        continue;
      now = opp[i].front();
      if (opp[now].front() == i && !lo[now]) {
        flag = true;
        opp[i].pop();
        opp[now].pop();
        lo[i] = lo[now] = true;
        sum += 2;
      }
      lo[i] = true;
    }
    cnt++;
    if (sum == N * (N - 1)) {
      out(cnt);
      return 0;
    }
    if (cnt > 100000) {
      out(N * (N - 1) / 2);
    };
  }
  out(-1);
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define out(str) cout << str << endl
#define ALL(a) (a).begin(), (a).end()
#define INF (1 << 29)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N, A, cnt, now, sum = 0;
bool flag = true, te[1000] = {};

int main() {
  cin >> N;
  queue<int> opp[N];
  rep(i, N) {
    rep(j, N - 1) {
      cin >> A;
      A--;
      opp[i].push(A);
    }
  }
  while (flag) {
    // out(cnt);
    flag = false;
    bool lo[N];
    // rep(i,N){
    //     lo[i]=false;
    // }
    memcpy(lo, te, sizeof(lo));
    rep(i, N) {
      if (lo[i] || opp[i].size() == 0)
        continue;
      now = opp[i].front();
      if (opp[now].front() == i && !lo[now]) {
        flag = true;
        opp[i].pop();
        opp[now].pop();
        lo[i] = lo[now] = true;
        sum += 2;
      }
      lo[i] = true;
    }
    cnt++;
    if (sum == N * (N - 1)) {
      out(cnt);
      return 0;
    }
    if (cnt > 100000) {
      out(N * (N - 1) / 2);
      return 0;
    };
  }
  out(-1);
}