Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

bool is_finised(vector<int> &poss, int n) {
  FOR_LT(i, 0, n) {
    if (poss[i] != n - 1)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  vector<vector<int>> as(n, vector<int>(n - 1));
  FOR_LT(i, 0, n) {
    FOR_LT(j, 0, n - 1) {
      cin >> as[i][j];
      as[i][j]--;
    }
  }

  vector<int> poss(n);
  int d = 0;
  while (!is_finised(poss, n)) {
    vector<bool> used(n);
    bool has_pair = false;

    FOR_LT(i, 0, n) {
      if (poss[i] == n - 1)
        continue;
      if (used[i])
        continue;

      int c = as[i][poss[i]];
      if (used[c])
        continue;
      if (as[c][poss[c]] == i) {
        poss[i]++;
        poss[c]++;
        used[i] = true;
        used[c] = true;
        has_pair = true;
      }
    }

    if (!has_pair) {
      cout << -1 << endl;
      return 0;
    }
    d++;
  }

  cout << d << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

bool is_finised(vector<int> &poss, int n) {
  FOR_LT(i, 0, n) {
    if (poss[i] != n - 1)
      return false;
  }
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;
  vector<vector<int>> as(n, vector<int>(n - 1));
  FOR_LT(i, 0, n) {
    FOR_LT(j, 0, n - 1) {
      cin >> as[i][j];
      as[i][j]--;
    }
  }

  vector<int> poss(n);
  int d = 0;

  queue<pair<int, int>> pair;
  {
    vector<bool> used(n);
    FOR_LT(i, 0, n) {
      if (poss[i] == n - 1)
        continue;
      if (used[i])
        continue;

      int c = as[i][poss[i]];
      if (used[c])
        continue;
      if (as[c][poss[c]] == i) {
        pair.push({i, c});
        used[i] = true;
        used[c] = true;
      }
    }
  }

  int p = 0;
  while (p != (n * (n - 1) / 2)) {
    if (pair.empty()) {
      cout << -1 << endl;
      return 0;
    }

    queue<int> ps;
    while (!pair.empty()) {
      auto c = pair.front();
      pair.pop();

      p++;

      poss[c.first]++;
      poss[c.second]++;
      ps.push(c.first);
      ps.push(c.second);
    }

    vector<bool> used(n);
    while (!ps.empty()) {
      int i = ps.front();
      ps.pop();

      if (poss[i] == n - 1)
        continue;
      if (used[i])
        continue;

      int c = as[i][poss[i]];
      if (used[c])
        continue;
      if (as[c][poss[c]] == i) {
        pair.push({i, c});
        used[i] = true;
        used[c] = true;
      }
    }

    d++;
  }

  while (!is_finised(poss, n)) {
    vector<bool> used(n);
    bool has_pair = false;

    FOR_LT(i, 0, n) {
      if (poss[i] == n - 1)
        continue;
      if (used[i])
        continue;

      int c = as[i][poss[i]];
      if (used[c])
        continue;
      if (as[c][poss[c]] == i) {
        poss[i]++;
        poss[c]++;
        used[i] = true;
        used[c] = true;
        has_pair = true;
      }
    }

    if (!has_pair) {
      cout << -1 << endl;
      return 0;
    }
    d++;
  }

  cout << d << endl;

  return 0;
}