Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<vector<int>> in(n + 1, vector<int>(n - 1));
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> in.at(i).at(j);
    }
  }
  vector<int> now(n + 1);
  vector<bool> check(n + 1, false);
  int cnt = 0;
  vector<set<int>> aite(n + 1);
  for (int i = 1; i < n + 1; i++) {
    aite.at(in.at(i).at(0)).insert(i);
  }
  int finish = 0;
  bool unable = true;
  while (finish < n) {
    unable = true;
    for (int i = 1; i < n + 1; i++) {
      if (check.at(i))
        continue;
      if (aite.at(i).empty())
        continue;
      auto iter = aite.at(i).begin();
      while (iter != aite.at(i).end()) {
        int j = *iter;
        if (check.at(j)) {
          iter++;
          continue;
        }
        if (aite.at(j).find(i) != aite.at(j).end()) {
          check.at(i) = check.at(j) = true;
          now.at(i)++;
          now.at(j)++;
          aite.at(i).erase(j);
          aite.at(j).erase(i);
          if (now.at(i) == n - 1)
            finish++;
          else
            aite.at(in.at(i).at(now.at(i))).insert(i);
          if (now.at(j) == n - 1)
            finish++;
          else
            aite.at(in.at(j).at(now.at(j))).insert(j);
          unable = false;
          break;
        }
        iter++;
      }
    }
    if (unable) {
      cout << -1 << endl;
      return 0;
    }
    cnt++;
    check = vector<bool>(n + 1, false);
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<vector<int>> in(n + 1, vector<int>(n - 1));
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> in.at(i).at(j);
    }
  }
  vector<int> now(n + 1);
  vector<bool> check(n + 1, false);
  int cnt = 0;
  vector<set<int>> aite(n + 1);
  for (int i = 1; i < n + 1; i++) {
    aite.at(in.at(i).at(0)).insert(i);
  }
  int finish = 0;
  bool unable = true;
  while (finish < n) {
    unable = true;
    for (int i = 1; i < n + 1; i++) {
      if (check.at(i))
        continue;
      if (aite.at(i).empty())
        continue;
      if (now.at(i) == n - 1)
        continue;
      int next = in.at(i).at(now.at(i));
      if (check.at(next))
        continue;
      if (aite.at(i).find(next) != aite.at(i).end()) {
        check.at(i) = check.at(next) = true;
        now.at(i)++;
        now.at(next)++;
        aite.at(i).erase(next);
        aite.at(next).erase(i);
        if (now.at(i) == n - 1)
          finish++;
        else
          aite.at(in.at(i).at(now.at(i))).insert(i);
        if (now.at(next) == n - 1)
          finish++;
        else
          aite.at(in.at(next).at(now.at(next))).insert(next);
        unable = false;
      }
    }
    if (unable) {
      cout << -1 << endl;
      return 0;
    }
    cnt++;
    check = vector<bool>(n + 1, false);
  }
  cout << cnt << endl;
  return 0;
}