prithivMLmods/Coder-Stat huggingface dataset:
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxnn 2000
int n;
int ry[maxnn][maxnn];
int ru[maxnn];
int d = 0;
int mark[maxnn];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      scanf("%d", &ry[i][j]);
      ru[i] = 1;
    }
  }
  while (1) {
    d++;
    for (int i = 1; i <= n; i++) {
      mark[i] = 0;
    }
    int tt = 0;
    int tr = 0;
    for (int i = 1; i <= n; i++) {
      if (ru[i] == n) {
        continue;
      }
      if (mark[i]) {
        continue;
      }
      if (ry[ry[i][ru[i]]][ru[ry[i][ru[i]]]] != i) {
        tt++;
      } else if (!mark[i] && !mark[ry[i][ru[i]]]) {
        ru[ry[i][ru[i]]]++;
        mark[i] = 1;
        mark[ry[i][ru[i]]] = 1;
        ru[i]++;
      }
    }
    for (int i = 1; i <= n; i++)
      if (ru[i] == n)
        tr++;
    if (tr == n) {
      cout << d;
      return 0;
    }
    if (n - tr == tt) {
      cout << -1;
      return 0;
    }
  }
}

Fixed submission:
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxnn 2000
ll n;
ll ry[maxnn][maxnn];
ll ru[maxnn];
ll d = 0;
ll mark[maxnn];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      scanf("%d", &ry[i][j]);
      ru[i] = 1;
    }
  }
  while (1) {
    d++;
    for (int i = 1; i <= n; i++) {
      mark[i] = 0;
    }
    int tt = 0;
    int tr = 0;
    for (int i = 1; i <= n; i++) {
      if (ru[i] == n) {
        continue;
      }
      if (mark[i]) {
        continue;
      }
      if (ry[ry[i][ru[i]]][ru[ry[i][ru[i]]]] != i) {
        tt++;
      } else if (!mark[i] && !mark[ry[i][ru[i]]]) {
        ru[ry[i][ru[i]]]++;
        mark[i] = 1;
        mark[ry[i][ru[i]]] = 1;
        ru[i]++;
      }
    }
    for (int i = 1; i <= n; i++)
      if (ru[i] == n)
        tr++;
    if (tr == n) {
      cout << d;
      return 0;
    }
    if (n - tr == tt) {
      cout << -1;
      return 0;
    }
  }
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;

int main() {
  LL n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  vector<int> c(n, 0);
  vector<int> x(n, 0);
  auto q = clock();
  for (int i = 1; i <= n * (n - 1) / 2 + 1; i++) {
    if (clock() - q >= 1900000) {
      cout << n * (n - 1) / 2 << endl;
      return 0;
    }
    bool f = false;
    bool t = false;
    for (int j = 0; j < n; j++) {
      if (c[j] == n - 1)
        continue;
      t = true;
      int en = a[j][c[j]];
      if (x[j] == i || x[en] == i)
        continue;
      if (c[en] == n - 1)
        continue;
      if (j == a[en][c[en]]) {
        c[j]++;
        c[en]++;
        x[j] = i;
        x[en] = i;
        f = true;
      }
    }
    if (!t) {
      cout << i - 1 << endl;
      return 0;
    }
    if (!f) {
      puts("-1");
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1LL << 60;
const int INF = 1 << 30;

int main() {
  LL n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  vector<int> c(n, 0);
  vector<int> x(n, 0);
  auto q = clock();
  for (int i = 1; i <= n * (n - 1) / 2 + 1; i++) {
    if (clock() - q >= 1000000) {
      cout << n * (n - 1) / 2 << endl;
      return 0;
    }
    bool f = false;
    bool t = false;
    for (int j = 0; j < n; j++) {
      if (c[j] == n - 1)
        continue;
      t = true;
      int en = a[j][c[j]];
      if (x[j] == i || x[en] == i)
        continue;
      if (c[en] == n - 1)
        continue;
      if (j == a[en][c[en]]) {
        c[j]++;
        c[en]++;
        x[j] = i;
        x[en] = i;
        f = true;
      }
    }
    if (!t) {
      cout << i - 1 << endl;
      return 0;
    }
    if (!f) {
      puts("-1");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
/***************************************************************
        File name: E.cpp
        Author: ljfcnyali
        Create time: 2019年09月01日 星期日 20时16分00秒
***************************************************************/
#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)
typedef long long LL;

const int maxn = 1010;

queue<int> Q[maxn];
int n;
bool vis[maxn];

int main() {
  scanf("%d", &n);
  REP(i, 1, n)
  REP(j, 1, n - 1) {
    int x;
    scanf("%d", &x);
    Q[i].push(x);
  }
  int i = 1;
  while (i) {
    bool flag = true;
    REP(i, 1, n) if (!Q[i].empty()) {
      flag = false;
      break;
    }
    if (flag == true) {
      printf("%d\n", i - 1);
      return 0;
    }
    mem(vis);
    REP(i, 1, n) {
      if (vis[i] || vis[Q[i].front()])
        continue;
      if (Q[Q[i].front()].front() == i) {
        vis[Q[i].front()] = vis[i] = true;
        Q[Q[i].front()].pop();
        Q[i].pop();
        flag = true;
      }
    }
    if (!flag) {
      printf("-1\n");
      return 0;
    }
    ++i;
  }
  return 0;
}

Fixed submission:
/***************************************************************
        File name: E.cpp
        Author: ljfcnyali
        Create time: 2019年09月01日 星期日 20时16分00秒
***************************************************************/
#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)
typedef long long LL;

const int maxn = 1010;

queue<int> Q[maxn];
int n;
bool vis[maxn];

int main() {
  scanf("%d", &n);
  REP(i, 1, n)
  REP(j, 1, n - 1) {
    int x;
    scanf("%d", &x);
    Q[i].push(x);
  }
  int i = 1;
  while (i) {
    bool flag = true;
    REP(i, 1, n) if (!Q[i].empty()) {
      flag = false;
      break;
    }
    if (flag == true) {
      printf("%d\n", i - 1);
      return 0;
    }
    mem(vis);
    REP(i, 1, n) {
      if (Q[i].empty() || Q[Q[i].front()].empty())
        continue;
      if (vis[i] || vis[Q[i].front()])
        continue;
      if (Q[Q[i].front()].front() == i) {
        vis[Q[i].front()] = vis[i] = true;
        Q[Q[i].front()].pop();
        Q[i].pop();
        flag = true;
      }
    }
    if (!flag) {
      printf("-1\n");
      return 0;
    }
    ++i;
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define long long long int

using namespace std;

// @author: pashka

int main() {
  ios::sync_with_stdio(false);

  int n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  };

  vector<int> c(n);
  vector<int> q;
  for (int i = 0; i < n; i++) {
    int x = a[i][0];
    if (a[x][0] == i) {
      q.push_back(i);
    }
  }
  int res = 0;
  while (!q.empty()) {
    res++;
    vector<int> q2;
    for (int i : q) {
      c[i]++;
      if (c[i] < n - 1) {
        int x = a[i][c[i]];
        if (a[x][c[x]] == i) {
          q2.push_back(i);
          q2.push_back(x);
        }
      }
    }
    q = q2;
  }
  for (int i = 0; i < n; i++) {
    if (c[i] < n - 1) {
      cout << -1;
      return 0;
    }
  }
  cout << res;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define long long long int

using namespace std;

// @author: pashka

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  };

  vector<int> c(n);
  vector<int> q;
  for (int i = 0; i < n; i++) {
    int x = a[i][0];
    if (a[x][0] == i) {
      q.push_back(i);
    }
  }
  int res = 0;
  while (!q.empty()) {
    res++;
    vector<int> q2;
    for (int i : q) {
      c[i]++;
      if (c[i] < n - 1) {
        int x = a[i][c[i]];
        if (a[x][c[x]] == i) {
          q2.push_back(i);
          q2.push_back(x);
        }
      }
    }
    q = q2;
  }
  for (int i = 0; i < n; i++) {
    if (c[i] < n - 1) {
      cout << -1;
      return 0;
    }
  }
  cout << res;

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;

int n;
int a[N][N];
int idx[N];

bool waiting[N][N];

int main() {
  scanf("%d", &n);

  vector<int> toCheck;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      scanf("%d", &a[i][j]);
      --a[i][j];
    }
    idx[i] = 0;
    toCheck.push_back(i);
  }

  int day = 0;
  bool can = true;
  while (can) {
    can = false;

    vector<int> ncheck;
    for (int u : toCheck) {
      int v = a[u][idx[u]];
      if (v < u)
        swap(u, v);

      if (waiting[u][v]) {
        can = true;
        waiting[u][v] = false;

        ++idx[u], ++idx[v];
        if (idx[u] < n - 1)
          ncheck.push_back(u);
        if (idx[v] < n - 1)
          ncheck.push_back(v);
      } else {
        waiting[u][v] = true;
      }
    }

    ncheck = toCheck;
    ++day;
  }

  for (int i = 0; i < n; ++i) {
    if (idx[i] != n - 1)
      day = -1;
  }

  printf("%d\n", day);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;

int n;
int a[N][N];
int idx[N];

bool waiting[N][N];

int main() {
  scanf("%d", &n);

  vector<int> toCheck;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      scanf("%d", &a[i][j]);
      --a[i][j];
    }
    idx[i] = 0;
    toCheck.push_back(i);
  }

  int day = 0;
  bool can = true;
  while (can) {
    can = false;

    vector<int> ncheck;
    for (int u : toCheck) {
      int v = a[u][idx[u]];
      if (v < u)
        swap(u, v);

      if (waiting[u][v]) {
        can = true;
        waiting[u][v] = false;

        ++idx[u], ++idx[v];
        if (idx[u] < n - 1)
          ncheck.push_back(u);
        if (idx[v] < n - 1)
          ncheck.push_back(v);
      } else {
        waiting[u][v] = true;
      }
    }

    toCheck = ncheck;
    if (can)
      ++day;
  }

  for (int i = 0; i < n; ++i) {
    if (idx[i] != n - 1)
      day = -1;
  }

  printf("%d\n", day);
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
/* author   : mpily :-()
title : Competing.cpp
time :Mon Sep  2 10:24:03 2019

*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int daycount = 0;
  cin >> n;
  vector<vector<int>> arr(n + 1);
  vector<int> tops(n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n - 1; j++) {
      int temp;
      cin >> temp;
      arr[i].push_back(temp);
    }
    tops[i] = 0;
  }
  int gamesCount = 0;
  while (gamesCount < (n * (n - 1)) / 2) {
    bool haschosen = 0;
    vector<int> toPlay;
    for (int i = 1; i <= n; i++) {
      if (tops[i] == n - 1)
        continue;
      int expected = arr[i][tops[i]];
      if (arr[expected][tops[expected]] == i && i < expected) {
        gamesCount += 1;
        // cout<<gamesCount<<" : "<<i<<expected<<"\n";
        toPlay.push_back(i);
        toPlay.push_back(expected);
        haschosen = 1;
      }
    }
    if (haschosen) {
      // cout<<"here";
      for (auto &a : toPlay) {
        tops[a] += 1;
      }
      daycount += 1;
    } else {
      cout << -1 << "\n";
      return 0;
    }
  }
  cout << daycount << "\n";
  return 0;
}


Fixed submission:
/* author   : mpily :-()
title : Competing.cpp
time :Mon Sep  2 10:24:03 2019

*/
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int daycount = 0;
  cin >> n;
  vector<vector<int>> arr(n + 1);
  vector<int> tops(n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n - 1; j++) {
      int temp;
      cin >> temp;
      arr[i].push_back(temp);
    }
    tops[i] = 0;
  }
  int gamesCount = 0;
  while (gamesCount < (n * (n - 1)) / 2) {
    bool haschosen = 0;
    vector<int> toPlay;
    for (int i = 1; i <= n; i++) {
      if (tops[i] == n - 1)
        continue;
      int expected = arr[i][tops[i]];
      if (arr[expected][tops[expected]] == i && i < expected) {
        gamesCount += 1;
        // cout<<gamesCount<<" : "<<i<<expected<<"\n";
        toPlay.push_back(i);
        toPlay.push_back(expected);
        haschosen = 1;
      }
    }
    if (haschosen) {
      // cout<<"here";
      for (auto &a : toPlay) {
        tops[a] += 1;
      }
      daycount += 1;
    } else {
      cout << -1 << "\n";
      return 0;
    }
    if (daycount >= 100000) {
      cout << (n * (n - 1)) / 2 << "\n";
      return 0;
    }
  }
  cout << daycount << "\n";
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int n;
queue<int> q[1001];
bool vis[1001];

int f() {
  memset(vis, 0, sizeof vis);
  bool flag = 0;
  for (int i = 1; i <= n; ++i) {
    if (vis[i])
      continue;
    if (q[i].empty())
      continue;
    int x = q[i].front();
    if (q[x].empty())
      continue;
    if (q[x].front() == i and !vis[i] and !vis[x]) {
      flag = 1;
      vis[x] = vis[i] = 1;
      q[x].pop();
      q[i].pop();
    }
  }
  if (!flag)
    return -1;
  for (int i = 1; i <= n; ++i)
    if (q[i].size())
      return 1;

  return 0;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      int x;
      scanf("%d", &x);
      q[i].push(x);
    }
  }

  int ans = 0;
  int ini = 1;
  int lim = 1e7;
  while (ini == 1 and lim--)
    ini = f(), ans++;
  if (ini == -1)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n;
queue<int> q[1001];
bool vis[1001];

int f() {
  memset(vis, 0, sizeof vis);
  bool flag = 0;
  for (int i = 1; i <= n; ++i) {
    if (vis[i])
      continue;
    if (q[i].empty())
      continue;
    int x = q[i].front();
    if (q[x].empty())
      continue;
    if (q[x].front() == i and !vis[i] and !vis[x]) {
      flag = 1;
      vis[x] = vis[i] = 1;
      q[x].pop();
      q[i].pop();
    }
  }
  if (!flag)
    return -1;
  for (int i = 1; i <= n; ++i)
    if (q[i].size())
      return 1;

  return 0;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      int x;
      scanf("%d", &x);
      q[i].push(x);
    }
  }

  int ans = 0;
  int ini = 1;
  int lim = 1000010;
  while (ini == 1 and lim--)
    ini = f(), ans++;
  if (ini == -1)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

constexpr int N = 1009;
int n, a[N][N];
vector<vector<int>> g(N *N);
vector<int> vis(N *N), mx(N *N);

inline int idx(int i, int j) {
  if (i > j)
    swap(i, j);
  return i * n + j;
}

void dfs(int u) {
  vis[u] = 1;
  mx[u] = 1;
  for (int v : g[u]) {
    if (vis[v] == 1) {
      cout << -1 << '\n';
      exit(0);
    } else
      dfs(v);
    mx[u] = max(mx[u], 1 + mx[v]);
  }
  vis[u] = 2;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n - 1; ++j) {
      cin >> a[i][j];
      if (j > 1) {
        g[idx(i, a[i][j - 1])].push_back(idx(i, a[i][j]));
      }
    }
  }
  for (int i = 1; i < N * N; ++i) {
    if (vis[i] == 0)
      dfs(i);
  }
  cout << *max_element(mx.begin(), mx.end()) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

constexpr int N = 1009;
int n, a[N][N];
vector<vector<int>> g(N *N);
vector<int> vis(N *N), mx(N *N);

inline int idx(int i, int j) {
  if (i > j)
    swap(i, j);
  return i * n + j;
}

void dfs(int u) {
  vis[u] = 1;
  mx[u] = 1;
  for (int v : g[u]) {
    if (vis[v] == 1) {
      cout << -1 << '\n';
      exit(0);
    } else if (vis[v] == 0)
      dfs(v);
    mx[u] = max(mx[u], 1 + mx[v]);
  }
  vis[u] = 2;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n - 1; ++j) {
      cin >> a[i][j];
      if (j > 1) {
        g[idx(i, a[i][j - 1])].push_back(idx(i, a[i][j]));
      }
    }
  }
  for (int i = 1; i < N * N; ++i) {
    if (vis[i] == 0)
      dfs(i);
  }
  cout << *max_element(mx.begin(), mx.end()) << '\n';
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N - 1));
  REP(i, N) {
    REP(j, N - 1) {
      cin >> A[i][j];
      A[i][j]--;
    }
    reverse(A[i].begin(), A[i].end());
  }
  vector<pair<int, int>> que;
  auto check = [&](int i) {
    if (A[i].size() == 0)
      return;
    int x = A[i].back();
    if (A[x].back() == i) {
      pair<int, int> match = {i, x};
      if (match.second < match.first)
        swap(match.first, match.second);
      que.push_back(match);
    }
  };
  REP(i, N) { check(i); }
  int day = 0;
  while (que.size()) {
    day++;
    que.erase(unique(que.begin(), que.end()), que.end());
    vector<pair<int, int>> pque;
    swap(pque, que);
    for (auto [x, y] : pque) {
      A[x].pop_back();
      A[y].pop_back();
    }

    for (auto [x, y] : pque) {
      check(x);
      check(y);
    }
  }
  REP(i, N) {
    if (A[i].size()) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << day << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N - 1));
  REP(i, N) {
    REP(j, N - 1) {
      cin >> A[i][j];
      A[i][j]--;
    }
    reverse(A[i].begin(), A[i].end());
  }
  vector<pair<int, int>> que;
  auto check = [&](int i) {
    if (A[i].size() == 0)
      return;
    int x = A[i].back();
    if (A[x].back() == i) {
      pair<int, int> match = {i, x};
      if (match.second < match.first)
        swap(match.first, match.second);
      que.push_back(match);
    }
  };
  REP(i, N) { check(i); }
  int day = 0;
  while (que.size()) {
    day++;
    sort(que.begin(), que.end());
    que.erase(unique(que.begin(), que.end()), que.end());
    vector<pair<int, int>> pque;
    swap(pque, que);
    for (auto [x, y] : pque) {
      A[x].pop_back();
      A[y].pop_back();
    }

    for (auto [x, y] : pque) {
      check(x);
      check(y);
    }
  }
  REP(i, N) {
    if (A[i].size()) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << day << endl;
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <vector>

#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
bool compare_by_b(pair<int, long long> a, pair<int, long long> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
bool flg = false;
int ch[1000][1000];
int hi[1001];
vector<queue<int>> A;
void solv(int a, int b) {
  // cout << "in a:" << a << " b:" << b << endl;
  if (ch[a][b] == 1) {
    flg = true;
    return;
  }
  ch[a][b] = 1;
  while (A[b].front() != a) {
    // cout << "in" << endl;
    solv(b, A[b].front());
    if (flg) {
      return;
    }
  }
  int m = max(hi[a], hi[b]);
  hi[a] = m + 1;
  hi[b] = m + 1;
  A[a].pop();
  A[b].pop();
  return;
}
int main() {
  int N;
  cin >> N;
  queue<int> qe;
  A.push_back(qe);
  rep(i, 0, N) {
    queue<int> qe;
    rep(r, 0, N - 1) {
      int a;
      cin >> a;
      qe.push(a);
    }
    A.push_back(qe);
  }
  rep(i, 0, N + 1) { hi[i] = 0; }
  rep(i, 1, N + 1) {
    while (!A[i].empty()) {
      // cout << "while" << endl;
      solv(i, A[i].front());
      if (flg) {
        break;
      }
    }
  }
  if (flg) {
    cout << -1 << endl;
  } else {
    int max = 0;
    // cout << "hi" << endl;
    rep(i, 1, N + 1) {
      // cout << hi[i] << endl;
      if (hi[i] > max) {
        max = hi[i];
      }
    }
    cout << max << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <vector>

#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
bool compare_by_b(pair<int, long long> a, pair<int, long long> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
bool flg = false;
int ch[1001][1001];
int hi[1001];
vector<queue<int>> A;
void solv(int a, int b) {
  // cout << "in a:" << a << " b:" << b << endl;
  if (ch[a][b] == 1) {
    flg = true;
    return;
  }
  ch[a][b] = 1;
  while (A[b].front() != a) {
    // cout << "in" << endl;
    solv(b, A[b].front());
    if (flg) {
      return;
    }
  }
  int m = max(hi[a], hi[b]);
  hi[a] = m + 1;
  hi[b] = m + 1;
  A[a].pop();
  A[b].pop();
  return;
}
int main() {
  int N;
  cin >> N;
  queue<int> qe;
  A.push_back(qe);
  rep(i, 0, N) {
    queue<int> qe;
    rep(r, 0, N - 1) {
      int a;
      cin >> a;
      qe.push(a);
    }
    A.push_back(qe);
  }
  rep(i, 0, N + 1) { hi[i] = 0; }
  rep(i, 1, N + 1) {
    while (!A[i].empty()) {
      // cout << "while" << endl;
      solv(i, A[i].front());
      if (flg) {
        break;
      }
    }
  }
  if (flg) {
    cout << -1 << endl;
  } else {
    int max = 0;
    // cout << "hi" << endl;
    rep(i, 1, N + 1) {
      // cout << hi[i] << endl;
      if (hi[i] > max) {
        max = hi[i];
      }
    }
    cout << max << endl;
  }
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;
int a[1000][1000];
int head[1001010];
struct Edge {
  int to;
  int next;
} edge[1001010];
int cnt;

void add_edge(int from, int to) {
  cnt++;
  edge[cnt].to = to;
  edge[cnt].next = head[from];
  head[from] = cnt;
}

int deg[1001010];
int exist[1001010];
int ans;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N - 1; j++)
      cin >> a[i][j];

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N - 2; j++) {
      int t1 = i, t2 = i;
      int x = a[i][j], y = a[i][j + 1];
      if (t1 > x)
        swap(t1, x);
      if (t2 > y)
        swap(t2, y);
      x = t1 * 1000 + x;
      y = t2 * 1000 + y;
      add_edge(x, y);
      deg[y]++;
      exist[x] = exist[y] = true;
    }
  }
  queue<int> q;
  for (int i = 1; i <= N * 1000 + N; i++)
    if (exist[i] && deg[i] == 0)
      q.push(i);

  while (!q.empty()) {
    int cnt = q.size();
    for (int i = 0; i < cnt; i++) {
      int t = q.front();
      q.pop();
      exist[t] = false;
      for (int e = head[t]; e; e = edge[e].next) {
        int to = edge[e].to;
        deg[to]--;
        if (deg[to] == 0)
          q.push(to);
      }
    }
    ans++;
  }
  for (int i = 1; i <= N * 1000 + N; i++)
    if (exist[i]) {
      cout << -1;
      return 0;
    }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int N;
int a[1010][1010];
int head[1001010];
struct Edge {
  int to;
  int next;
} edge[1001010];
int cnt;

void add_edge(int from, int to) {
  cnt++;
  edge[cnt].to = to;
  edge[cnt].next = head[from];
  head[from] = cnt;
}

int deg[1001010];
int exist[1001010];
int ans;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N - 1; j++)
      cin >> a[i][j];

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N - 2; j++) {
      int t1 = i, t2 = i;
      int x = a[i][j], y = a[i][j + 1];
      if (t1 > x)
        swap(t1, x);
      if (t2 > y)
        swap(t2, y);
      x = t1 * 1000 + x;
      y = t2 * 1000 + y;
      add_edge(x, y);
      deg[y]++;
      exist[x] = exist[y] = true;
    }
  }
  queue<int> q;
  for (int i = 1; i <= N * 1000 + N; i++)
    if (exist[i] && deg[i] == 0)
      q.push(i);

  while (!q.empty()) {
    int cnt = q.size();
    for (int i = 0; i < cnt; i++) {
      int t = q.front();
      q.pop();
      exist[t] = false;
      for (int e = head[t]; e; e = edge[e].next) {
        int to = edge[e].to;
        deg[to]--;
        if (deg[to] == 0)
          q.push(to);
      }
    }
    ans++;
  }
  for (int i = 1; i <= N * 1000 + N; i++)
    if (exist[i]) {
      cout << -1;
      return 0;
    }
  cout << ans;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

const int NMAX = 1000;

int N, f;
int ord[NMAX + 5][NMAX + 5];
int m[NMAX + 5][NMAX + 5];

vector<int> g[NMAX * NMAX + 5];
vector<int> w;

int ind[NMAX + 5];

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < N; j++) {
      cin >> ord[i][j];

      if (!m[min(i, ord[i][j])][max(i, ord[i][j])])
        m[min(i, ord[i][j])][max(i, ord[i][j])] = ++f;
    }

    for (int j = 2; j < N; j++) {
      /// trasam muchie de la i, ord[i][j - 1] la i, ord[i][j]
      g[m[min(i, ord[i][j - 1])][max(i, ord[i][j - 1])]].push_back(
          m[min(i, ord[i][j])][max(i, ord[i][j])]);
      ind[m[min(i, ord[i][j])][max(i, ord[i][j])]]++;
    }
  }

  for (int i = 1; i <= f; i++)
    if (!ind[i])
      w.push_back(i);

  int days = 0, processed = 0;

  while (!w.empty()) {
    days++;
    processed += w.size();

    vector<int> w2;

    for (auto it : w) {
      for (auto it2 : g[it]) {
        ind[it2]--;
        if (ind[it2] == 0)
          w2.push_back(it2);
      }
    }

    w = w2;
  }

  if (processed == f)
    cout << days << '\n';
  else
    cout << -1 << '\n';

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

const int NMAX = 1000;

int N, f;
int ord[NMAX + 5][NMAX + 5];
int m[NMAX + 5][NMAX + 5];

vector<int> g[NMAX * NMAX + 5];
vector<int> w;

int ind[NMAX * NMAX + 5];

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < N; j++) {
      cin >> ord[i][j];

      if (!m[min(i, ord[i][j])][max(i, ord[i][j])])
        m[min(i, ord[i][j])][max(i, ord[i][j])] = ++f;
    }

    for (int j = 2; j < N; j++) {
      /// trasam muchie de la i, ord[i][j - 1] la i, ord[i][j]
      g[m[min(i, ord[i][j - 1])][max(i, ord[i][j - 1])]].push_back(
          m[min(i, ord[i][j])][max(i, ord[i][j])]);
      ind[m[min(i, ord[i][j])][max(i, ord[i][j])]]++;
    }
  }

  for (int i = 1; i <= f; i++)
    if (!ind[i])
      w.push_back(i);

  int days = 0, processed = 0;

  while (!w.empty()) {
    days++;
    processed += w.size();

    vector<int> w2;

    for (auto it : w) {
      for (auto it2 : g[it]) {
        ind[it2]--;
        if (ind[it2] == 0)
          w2.push_back(it2);
      }
    }

    w = w2;
  }

  if (processed == f)
    cout << days << '\n';
  else
    cout << -1 << '\n';

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  queue<int> A[N];
  rep(i, N) rep(j, N - 1) {
    int B;
    cin >> B;
    A[i].push(B);
  }
  int ans = 0;
  int used[N];
  memset(used, 0, sizeof(used));
  while (true) {
    bool ok = false;
    rep(j, N) {
      if (A[j].front() - 1 >= 0 && A[A[j].front() - 1].front() == j + 1 &&
          used[j] == 0 && used[A[j].front() - 1] == 0) {
        used[j] = 1;
        used[A[j].front() - 1] = 1;
        A[A[j].front() - 1].pop();
        A[j].pop();
        ok = true;
      }
    }
    if (!ok)
      break;
    rep(i, N) used[i] = 0;
    ans++;
  }
  rep(i, N) {
    if (A[i].size() != 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << '\n';
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  queue<int> A[N];
  rep(i, N) rep(j, N - 1) {
    int B;
    cin >> B;
    A[i].push(B);
  }
  int ans = 0;
  int used[N];
  memset(used, 0, sizeof(used));
  while (true) {
    bool ok = false;
    rep(j, N) {
      if (A[j].front() - 1 >= 0 && A[A[j].front() - 1].front() == j + 1 &&
          used[j] == 0 && used[A[j].front() - 1] == 0) {
        used[j] = 1;
        used[A[j].front() - 1] = 1;
        A[A[j].front() - 1].pop();
        A[j].pop();
        ok = true;
      }
    }
    if (!ok)
      break;
    rep(i, N) used[i] = 0;
    ans++;
  }
  rep(i, N) {
    if (A[i].size() != 0) {
      cout << -1 << '\n';
      return 0;
    }
  }
  cout << ans << '\n';
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int N = 1e6 + 6;
int n, in[N], nxt[N][2], b[1001];

inline int idx(int a, int b) {
  if (a > b)
    swap(a, b);
  return a * n + b;
}

int main() {
  memset(nxt, -1, sizeof(nxt));
  memset(in, 0, sizeof(nxt));
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j < n; ++j) {
      scanf("%d", b + j);
      --b[j];
    }
    for (int j = 1; j < n - 1; ++j) {
      int m = idx(i, b[j + 1]);
      ++in[m];
      if (i < b[j])
        nxt[i * n + b[j]][0] = m;
      else
        nxt[b[j] * n + i][1] = m;
    }
    for (int j = 0; j <= i; ++j)
      in[i * n + j] = -1;
  }
  int cnt = 0, ans = 0;
  queue<int> q;
  for (int i = n * n - 1; ~i; --i)
    if (in[i] == 0)
      q.push(i);

  while (q.size()) {
    ++ans;
    int m = q.size();
    cnt += m;
    while (m--) {
      int i = q.front();
      q.pop();
      if (~nxt[i][0] && 0 == --in[nxt[i][0]])
        q.push(nxt[i][0]);
      if (~nxt[i][1] && 0 == --in[nxt[i][1]])
        q.push(nxt[i][1]);
    }
  }
  if (cnt < n * (n - 1) / 2)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int N = 1e6 + 6;
int n, in[N], nxt[N][2], b[1001];

inline int idx(int a, int b) {
  if (a > b)
    swap(a, b);
  return a * n + b;
}

int main() {
  memset(nxt, -1, sizeof(nxt));
  memset(in, 0, sizeof(in));
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j < n; ++j) {
      scanf("%d", b + j);
      --b[j];
    }
    for (int j = 1; j < n - 1; ++j) {
      int m = idx(i, b[j + 1]);
      ++in[m];
      if (i < b[j])
        nxt[i * n + b[j]][0] = m;
      else
        nxt[b[j] * n + i][1] = m;
    }
    for (int j = 0; j <= i; ++j)
      in[i * n + j] = -1;
  }
  int cnt = 0, ans = 0;
  queue<int> q;
  for (int i = n * n - 1; ~i; --i)
    if (in[i] == 0)
      q.push(i);

  while (q.size()) {
    ++ans;
    int m = q.size();
    cnt += m;
    while (m--) {
      int i = q.front();
      q.pop();
      if (~nxt[i][0] && 0 == --in[nxt[i][0]])
        q.push(nxt[i][0]);
      if (~nxt[i][1] && 0 == --in[nxt[i][1]])
        q.push(nxt[i][1]);
    }
  }
  if (cnt < n * (n - 1) / 2)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

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

  vector<vector<int>> edge(10000000, vector<int>());
  vector<int> indeg(10000000, 0);
  int n, a;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int s, t;
    for (int j = 0; j < n - 1; j++) {
      cin >> a;

      t = min(i, a) * 10000 + max(i, a);
      if (j != 0) {
        edge[s].push_back(t);
        indeg[t]++;
      }
      s = t;
    }
  }

  queue<int> qu;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (indeg[i * 10000 + j] == 0)
        qu.push(i * 10000 + j);
    }
  }

  int day = 1, match = 0;
  vector<int> used(10000000, 0), zero(10000000, 0);
  while (!qu.empty()) {
    int tmp = qu.front();
    qu.pop();
    int x = tmp / 10000, y = tmp % 10000;

    // cout << tmp << endl;

    for (auto v : edge[tmp]) {
      // cout << v << " " << indeg[v] << endl;
      if (--indeg[v] == 0)
        qu.push(v);
    }

    if (used[x] || used[y]) {
      day++;
      used = zero;
    }

    used[x] = used[y] = 1;
    match++;
  }

  cout << ((match == n * (n - 1) / 2) ? day : -1) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

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

  vector<vector<int>> edge(10000000, vector<int>());
  vector<int> indeg(10000000, 0);
  int n, a;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int s, t;
    for (int j = 0; j < n - 1; j++) {
      cin >> a;

      t = min(i, a) * 10000 + max(i, a);
      if (j != 0) {
        edge[s].push_back(t);
        indeg[t]++;
      }
      s = t;
    }
  }

  queue<int> qu;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (indeg[i * 10000 + j] == 0)
        qu.push(i * 10000 + j);
    }
  }

  int day = 1, match = 0;
  vector<int> used(1001, 0), zero(1001, 0);
  while (!qu.empty()) {
    int tmp = qu.front();
    qu.pop();
    int x = tmp / 10000, y = tmp % 10000;

    // cout << tmp << endl;

    for (auto v : edge[tmp]) {
      // cout << v << " " << indeg[v] << endl;
      if (--indeg[v] == 0)
        qu.push(v);
    }

    if (used[x] || used[y]) {
      day++;
      used = zero;
    }

    used[x] = used[y] = 1;
    match++;
  }

  cout << ((match == n * (n - 1) / 2) ? day : -1) << endl;

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main() {
  //    ios::sync_with_stdio(false);
  //    cin.tie(0);
  int N;
  cin >> N;
  int A[1010][1010];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      cin >> a;
      a--;
      A[i][j] = a;
    }
  }

  vector<int> cur(N);

  int cnt = 0;
  while (1) {

    bool nopair = true;

    bool used[1010];
    for (int i = 0; i < 1010; i++)
      used[i] = false;

    for (int i = 0; i < N; i++) {
      if (cur[i] == N - 1)
        continue;
      int aite = A[i][cur[i]];

      if (used[i] || used[aite])
        continue;
      if (i == A[aite][cur[aite]]) {
        cur[i]++;
        cur[aite]++;
        used[i] = true;
        used[aite] = true;
        nopair = false;
      }
    }

    if (nopair) {
      cout << -1 << endl;
      return 0;
    }

    cnt++;

    bool allend = true;
    for (int i = 0; i < N; i++) {
      if (cur[i] != N - 1)
        allend = false;
    }

    if (allend) {
      cout << cnt << endl;
      return 0;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int A[1010][1010];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      cin >> a;
      a--;
      A[i][j] = a;
    }
  }

  vector<int> cur(N);

  int cnt = 0;
  while (1) {

    bool nopair = true;

    bool used[1010];
    for (int i = 0; i < 1010; i++)
      used[i] = false;

    for (int i = 0; i < N; i++) {
      if (cur[i] == N - 1)
        continue;
      int aite = A[i][cur[i]];

      if (used[i] || used[aite])
        continue;
      if (i == A[aite][cur[aite]]) {
        cur[i]++;
        cur[aite]++;
        used[i] = true;
        used[aite] = true;
        nopair = false;
      }
    }

    if (nopair) {
      cout << -1 << endl;
      return 0;
    }

    cnt++;

    bool allend = true;
    for (int i = 0; i < N; i++) {
      if (cur[i] != N - 1)
        allend = false;
    }

    if (allend) {
      cout << cnt << endl;
      return 0;
    }
  }

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main() {
  //    ios::sync_with_stdio(false);
  //    cin.tie(0);
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N - 1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      cin >> a;
      a--;
      A[i][j] = a;
    }
  }

  vector<int> cur(N);

  int cnt = 0;
  while (1) {

    bool nopair = true;

    bool used[1010] = {};

    for (int i = 0; i < N; i++) {
      if (cur[i] == N - 1)
        continue;
      int aite = A[i][cur[i]];

      if (used[i] || used[aite])
        continue;
      if (i == A[aite][cur[aite]]) {
        cur[i]++;
        cur[aite]++;
        used[i] = true;
        used[aite] = true;
        nopair = false;
      }
    }

    if (nopair) {
      cout << -1 << endl;
      return 0;
    }

    cnt++;

    bool allend = true;
    for (int i = 0; i < N; i++) {
      if (cur[i] != N - 1)
        allend = false;
    }

    if (allend) {
      cout << cnt << endl;
      return 0;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main() {
  //    ios::sync_with_stdio(false);
  //    cin.tie(0);
  int N;
  cin >> N;
  int A[1010][1010];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      cin >> a;
      a--;
      A[i][j] = a;
    }
  }

  vector<int> cur(N);

  int cnt = 0;
  while (1) {

    bool nopair = true;

    bool used[1010] = {};

    for (int i = 0; i < N; i++) {
      if (cur[i] == N - 1)
        continue;
      int aite = A[i][cur[i]];

      if (used[i] || used[aite])
        continue;
      if (i == A[aite][cur[aite]]) {
        cur[i]++;
        cur[aite]++;
        used[i] = true;
        used[aite] = true;
        nopair = false;
      }
    }

    if (nopair) {
      cout << -1 << endl;
      return 0;
    }

    cnt++;

    bool allend = true;
    for (int i = 0; i < N; i++) {
      if (cur[i] != N - 1)
        allend = false;
    }

    if (allend) {
      cout << cnt << endl;
      return 0;
    }
  }

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

typedef pair<int, int> P;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N - 1));
  rep(i, N) {
    rep(j, N - 1) {
      cin >> a[i][j];
      a[i][j]--;
    }
    reverse(a[i].begin(), a[i].end());
  }

  vector<P> q;
  auto check = [&](int i) {
    if (a[i].size() == 0)
      return;            // 対戦する試合がない場合
    int j = a[i].back(); // 対戦相手を取得
    if (a[j].size() == 0)
      return; // 対戦相手の対戦がない場合

    if (a[j].back() == i) { // i,jが対戦できるとき
      P p(i, j);
      if (p.second < p.first)
        swap(p.first, p.second);
      q.push_back(p);
    }
  };
  rep(i, N) { check(i); }
  int day = 0;
  while (q.size() >= 0) {
    day++;

    // 重複を削除する
    sort(q.begin(), q.end());
    q.erase(unique(q.begin(), q.end()), q.end());

    vector<P> prevQ;
    swap(prevQ, q);
    for (P p : prevQ) {
      int i = p.first;
      int j = p.second;
      // 試合を消化
      a[i].pop_back();
      a[j].pop_back();
    }

    for (P p : prevQ) {
      int i = p.first;
      int j = p.second;
      // 次の試合ができるかチェック
      check(i);
      check(j);
    }
  }

  rep(i, N) {
    if (a[i].size() != 0) { // 未消化の試合がある場合
      puts("-1");
      return 0;
    }
  }
  cout << day << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

typedef pair<int, int> P;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N - 1));
  rep(i, N) {
    rep(j, N - 1) {
      cin >> a[i][j];
      a[i][j]--;
    }
    reverse(a[i].begin(), a[i].end());
  }

  vector<P> q;
  auto check = [&](int i) {
    if (a[i].size() == 0)
      return;            // 対戦する試合がない場合
    int j = a[i].back(); // 対戦相手を取得
    if (a[j].size() == 0)
      return; // 対戦相手の対戦がない場合

    if (a[j].back() == i) { // i,jが対戦できるとき
      P p(i, j);
      if (p.second < p.first)
        swap(p.first, p.second);
      q.push_back(p);
    }
  };
  rep(i, N) { check(i); }
  int day = 0;
  while (q.size() > 0) {
    day++;

    // 重複を削除する
    sort(q.begin(), q.end());
    q.erase(unique(q.begin(), q.end()), q.end());

    vector<P> prevQ;
    swap(prevQ, q);
    for (P p : prevQ) {
      int i = p.first;
      int j = p.second;
      // 試合を消化
      a[i].pop_back();
      a[j].pop_back();
    }

    for (P p : prevQ) {
      int i = p.first;
      int j = p.second;
      // 次の試合ができるかチェック
      check(i);
      check(j);
    }
  }

  rep(i, N) {
    if (a[i].size() != 0) { // 未消化の試合がある場合
      puts("-1");
      return 0;
    }
  }
  cout << day << endl;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(void) {
  int N, n;
  cin >> N;
  vector<queue<int>> stacks(N, queue<int>());
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> n;
      stacks.at(i).push(n);
    }
  }
  int count = 1;
  int pop_count = 0;
  stack<int> push_s;
  set<int> need_check;
  for (int i = 0; i < N; i++) {
    need_check.insert(i);
  }
  while (true) {
    set<int> need_check_more;
    for (auto itr = need_check.begin(); itr != need_check.end(); itr++) {
      int i = *itr;
      if (stacks.at(i).size() == 0) {
        continue;
      }
      n = stacks.at(i).front();
      if (stacks.at(n - 1).front() == i + 1) {
        if (need_check_more.count(i) == 0) {
          push_s.push(i);
          need_check_more.insert(i);
        }
        if (need_check_more.count(n - 1) == 0) {
          push_s.push(n - 1);
          need_check_more.insert(n - 1);
        }
      }
    }
    if (push_s.size() == 0) {
      cout << -1;
      return 0;
    }
    while (push_s.size() > 0) {
      n = push_s.top();
      push_s.pop();
      stacks.at(n).pop();
      pop_count++;
    }
    if (pop_count == N * (N - 1)) {
      cout << count;
      return 0;
    }
    count++;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(void) {
  int N, n;
  cin >> N;
  vector<queue<int>> stacks(N, queue<int>());
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> n;
      stacks.at(i).push(n);
    }
  }
  int count = 1;
  int pop_count = 0;
  stack<int> push_s;
  set<int> need_check;
  for (int i = 0; i < N; i++) {
    need_check.insert(i);
  }
  while (true) {
    set<int> need_check_more;
    for (auto itr = need_check.begin(); itr != need_check.end(); itr++) {
      int i = *itr;
      if (stacks.at(i).size() == 0) {
        continue;
      }
      n = stacks.at(i).front();
      if (stacks.at(n - 1).front() == i + 1) {
        if (need_check_more.count(i) == 0) {
          push_s.push(i);
          need_check_more.insert(i);
        }
        if (need_check_more.count(n - 1) == 0) {
          push_s.push(n - 1);
          need_check_more.insert(n - 1);
        }
      }
    }
    need_check = need_check_more;
    if (push_s.size() == 0) {
      cout << -1;
      return 0;
    }
    while (push_s.size() > 0) {
      n = push_s.top();
      push_s.pop();
      stacks.at(n).pop();
      pop_count++;
    }
    if (pop_count == N * (N - 1)) {
      cout << count;
      return 0;
    }
    count++;
  }

  return 0;
}