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

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main(void) {

  int n;
  int a[1000][999];
  int g[1000];
  int check[1001];
  int day;
  int f;

  int i, j;

  int match;
  int index;

  scanf("%d", &n);

  // プレイヤー番号は下駄をはかせて使う；
  for (i = 1; i < n + 1; i++) {
    g[i] = 0;
    for (j = 0; j < n - 1; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  f = 1;
  day = 0;
  while (f == 1) {

    f = 0;
    index = 0;
    check[0] = -1;

    for (i = 1; i < n + 1; i++) {
      // g[i]がプレイヤーiの次のゲーム数

      match = a[i][g[i]];

      // 番号が大きい方が相手（＝初めての参照）
      if (match > i) {
        if (a[match][g[match]] == i) {
          check[index] = i;
          check[index + 1] = match;
          check[index + 2] = -1; // 番兵更新
          index += 2;
          f = 1;
        }
      }
    }

    index = 0;
    while (check[index] != -1) {
      g[check[index]]++;
      index++;
    };
    day++;
  }

  f = 0;
  for (i = 1; i < n + 1; i++) {
    if (g[i] != n - 1) {
      f = 1;
    }
  }

  if (f == 1) {
    printf("-1\n");
  } else {
    printf("%d\n", day - 1);
  }

  return 0;
}

Fixed submission:
#include <stdio.h>

int main(void) {

  int n;
  int a[1001][1000];
  int g[1001];
  int check[1002];
  int day;
  int f;

  int i, j;

  int match;
  int index;

  scanf("%d", &n);

  // プレイヤー番号は下駄をはかせて使う；
  for (i = 1; i < n + 1; i++) {
    g[i] = 0;
    for (j = 0; j < n - 1; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  f = 1;
  day = 0;
  while (f == 1) {

    f = 0;
    index = 0;
    check[0] = -1;

    for (i = 1; i < n + 1; i++) {
      // g[i]がプレイヤーiの次のゲーム数

      match = a[i][g[i]];

      // 番号が大きい方が相手（＝初めての参照）
      if (match > i) {
        if (a[match][g[match]] == i) {
          check[index] = i;
          check[index + 1] = match;
          check[index + 2] = -1; // 番兵更新
          index += 2;
          f = 1;
        }
      }
    }

    index = 0;
    while (check[index] != -1) {
      g[check[index]]++;
      index++;
    };
    day++;
  }

  f = 0;
  for (i = 1; i < n + 1; i++) {
    if (g[i] != n - 1) {
      f = 1;
    }
  }

  if (f == 1) {
    printf("-1\n");
  } else {
    printf("%d\n", day - 1);
  }

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n, a[1005][1005], now[1005], ans = 0, tmp[1005], ok;

int read() {
  int s = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    s = s * 10 + c - '0', c = getchar();
  return s;
}

int main(void) {
  n = read();
  for (int i = 1; i <= n; i++) {
    now[i] = 1;
    tmp[i] = -1;
    for (int j = 1; j < n; j++)
      a[i][j] = read();
  }
  ok = n * (n - 1) / 2;
  while (1) {
    bool check = 0;
    int p = 0;
    for (int i = 1; i <= n; i++) {
      int u = a[i][now[i]];
      if (tmp[u] == ans || tmp[i] == ans)
        continue;
      if (a[u][now[u]] != i)
        continue;
      check = 1;
      tmp[i] = ans;
      tmp[u] = ans;
      now[u]++;
      now[i]++;
      p += 2;
      ok--;
      if (n - p < 2)
        break;
    }
    ans++;
    if (ok == 0)
      break;
    if (check == 0 && ok != 0) {
      printf("-1\n");
      return 0;
    }
  }
  printf("%d", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n, a[10005][10005], now[10005], ans = 0, tmp[10005], ok;

int read() {
  int s = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    s = s * 10 + c - '0', c = getchar();
  return s;
}

int main(void) {
  n = read();
  for (int i = 1; i <= n; i++) {
    now[i] = 1;
    tmp[i] = -1;
    for (int j = 1; j < n; j++)
      a[i][j] = read();
  }
  ok = n * (n - 1) / 2;
  while (1) {
    bool check = 0;
    int p = 0;
    for (int i = 1; i <= n; i++) {
      int u = a[i][now[i]];
      if (tmp[u] == ans || tmp[i] == ans)
        continue;
      if (a[u][now[u]] != i)
        continue;
      check = 1;
      tmp[i] = ans;
      tmp[u] = ans;
      now[u]++;
      now[i]++;
      p += 2;
      ok--;
      if (n - p < 2)
        break;
    }
    ans++;
    if (ok == 0)
      break;
    if (check == 0 && ok != 0) {
      printf("-1\n");
      return 0;
    }
  }
  printf("%d", ans);
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  vector<int> head(n);
  rep(i, n) {
    rep(j, n - 1) {
      int x;
      cin >> x;
      x--;
      a[i][j] = x;
    }
  }
  int turn = 0;
  vector<int> used(n, -1);
  while (true) {
    bool ng = true;
    int empty_cnt = 0;
    rep(i, n) {
      if (head[i] == n - 1) {
        empty_cnt++;
        continue;
      }
      int j = a[i][head[i]];
      if (used[i] == turn || used[j] == turn)
        continue;
      if (a[j][head[j]] == i) {
        head[i]++;
        head[j]++;
        used[i] = used[j] = turn;
        ng = false;
      }
    }
    if (empty_cnt == n)
      break;
    if (ng) {
      cout << -1 << endl;
      return 0;
    }
    turn++;
  }
  cout << turn << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int a[1010][1010] = {};
  int head[1010] = {};
  rep(i, n) {
    rep(j, n - 1) {
      int x;
      cin >> x;
      x--;
      a[i][j] = x;
    }
  }
  int turn = 0;
  vector<int> used(n, -1);
  while (true) {
    bool ng = true;
    int empty_cnt = 0;
    rep(i, n) {
      if (head[i] == n - 1) {
        empty_cnt++;
        continue;
      }
      int j = a[i][head[i]];
      if (used[i] == turn || used[j] == turn)
        continue;
      if (a[j][head[j]] == i) {
        head[i]++;
        head[j]++;
        used[i] = used[j] = turn;
        ng = false;
      }
    }
    if (empty_cnt == n)
      break;
    if (ng) {
      cout << -1 << endl;
      return 0;
    }
    turn++;
  }
  cout << turn << endl;

  return 0;
}
Problem p02925 (C++)

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

const int MAX_N = 1000;
const int MAX_V = MAX_N * (MAX_N - 1) / 2;
int id[MAX_N][MAX_N];
int a[MAX_N][MAX_N];
vector<int> edge[MAX_V];

int toId(int i, int j) {
  if (i > j)
    swap(i, j);
  return id[i][j];
}

int depth[MAX_V];
int dfs(int v) {
  depth[v] = 1;
  for (int u : edge[v]) {
    depth[v] = max(depth[v], dfs(u) + 1);
  }
  return depth[v];
}

int main() {
  int n;
  cin >> n;
  int numV = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      id[i][j] = numV++;
    }
  }

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 2; j++) {
      edge[a[i][j]].push_back(a[i][j + 1]);
      deg[a[i][j + 1]]++;
    }
  }

  // DAGかどうか判定
  stack<int> st;
  for (int i = 0; i < numV; i++) {
    if (deg[i] == 0)
      st.push(i);
  }
  int ans = 0;
  while (!st.empty()) {
    int i = st.top();
    st.pop();
    ans++;
    for (int j : edge[i]) {
      if (--deg[j] == 0) {
        st.push(j);
      }
    }
  }

  if (ans != numV) {
    cout << -1 << endl;
    return 0;
  }

  ans = 0;
  int tmp;
  for (int i = 0; i < numV; i++) {
    tmp = dfs(i);
    if (tmp == -1) {
      cout << -1 << endl;
      return 0;
    } else if (tmp > ans)
      ans = tmp;
  }
  cout << ans << endl;
  return 0;
}

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

const int MAX_N = 1000;
const int MAX_V = MAX_N * (MAX_N - 1) / 2;
int id[MAX_N][MAX_N];
int a[MAX_N][MAX_N];
vector<int> edge[MAX_V];

int toId(int i, int j) {
  if (i > j)
    swap(i, j);
  return id[i][j];
}

int depth[MAX_V];
int dfs(int v) {
  if (depth[v] > 0)
    return depth[v];
  depth[v] = 1;
  for (int u : edge[v]) {
    depth[v] = max(depth[v], dfs(u) + 1);
  }
  return depth[v];
}

int main() {
  int n;
  cin >> n;
  int numV = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      id[i][j] = numV++;
    }
  }

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 2; j++) {
      edge[a[i][j]].push_back(a[i][j + 1]);
      deg[a[i][j + 1]]++;
    }
  }

  // DAGかどうか判定
  stack<int> st;
  for (int i = 0; i < numV; i++) {
    if (deg[i] == 0)
      st.push(i);
  }
  int ans = 0;
  while (!st.empty()) {
    int i = st.top();
    st.pop();
    ans++;
    for (int j : edge[i]) {
      if (--deg[j] == 0) {
        st.push(j);
      }
    }
  }

  if (ans != numV) {
    cout << -1 << endl;
    return 0;
  }

  ans = 0;
  int tmp;
  for (int i = 0; i < numV; i++) {
    tmp = dfs(i);
    if (tmp == -1) {
      cout << -1 << endl;
      return 0;
    } else if (tmp > ans)
      ans = tmp;
  }
  cout << ans << endl;
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef int64_t Int;
typedef pair<int, int> P;

int N;
vector<deque<int>> A(N);

vector<P> match;

// kさんが試合をできればmatchに代入する
void check(int i) {
  if (A[i].size() == 0)
    return;

  int j = A[i].front();
  if (A[j].front() == i) {
    if (i > j)
      swap(i, j);
    match.push_back(P(i, j));
  }
}

int main() {
  cin >> N;

  rep(i, N) rep(j, N - 1) {
    int a;
    cin >> a;
    a--;
    A[i].push_back(a);
  }

  rep(i, N) check(i);

  int ans = 0;
  while (!match.empty()) {
    ans++;

    sort(match.begin(), match.end());
    match.erase(unique(match.begin(), match.end()), match.end());

    vector<P> prev_match;
    swap(prev_match, match);

    for (P p : prev_match) {
      int i = p.first, j = p.second;
      A[i].pop_front();
      A[j].pop_front();
    }

    for (P p : prev_match) {
      int i = p.first, j = p.second;
      check(i);
      check(j);
    }
  }

  rep(i, N) {
    if (A[i].size() != 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef int64_t Int;
typedef pair<int, int> P;

int N;
vector<deque<int>> A(1010);

vector<P> match;

// kさんが試合をできればmatchに代入する
void check(int i) {
  if (A[i].size() == 0)
    return;

  int j = A[i].front();
  if (A[j].front() == i) {
    if (i > j)
      swap(i, j);
    match.push_back(P(i, j));
  }
}

int main() {
  cin >> N;

  rep(i, N) rep(j, N - 1) {
    int a;
    cin >> a;
    a--;
    A[i].push_back(a);
  }

  rep(i, N) check(i);

  int ans = 0;
  while (!match.empty()) {
    ans++;

    sort(match.begin(), match.end());
    match.erase(unique(match.begin(), match.end()), match.end());

    vector<P> prev_match;
    swap(prev_match, match);

    for (P p : prev_match) {
      int i = p.first, j = p.second;
      A[i].pop_front();
      A[j].pop_front();
    }

    for (P p : prev_match) {
      int i = p.first, j = p.second;
      check(i);
      check(j);
    }
  }

  rep(i, N) {
    if (A[i].size() != 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << ans << endl;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

static const int MAXN = 1e+3;
static const int MAXV = MAXN * (MAXN - 1) / 2;
int mat[MAXN][MAXN], dp[MAXV];
vector<int> adj[MAXN];
bool visited[MAXV], calculated[MAXV];

// max length of path from s
int dfs(int u) {
  if (visited[u]) {
    if (!calculated[u])
      return -1;
    return dp[u];
  }
  visited[u] = true;
  dp[u] = 1;
  for (int v : adj[u]) {
    int res = dfs(v);
    if (res == -1)
      return -1;
    dp[u] = max(dp[u], res + 1);
  }
  calculated[u] = true;
  return dp[u];
}

int main() {
  int n;
  cin >> n;
  int cnt = 0, v = n * (n - 1) / 2;
  rep(i, n) for (int j = i + 1; j < n; j++) {
    mat[i][j] = mat[j][i] = cnt;
    cnt++;
  }
  int a, prev_a;
  rep(i, n) {
    scanf("%d", &prev_a);
    prev_a--;
    rep(_, n - 2) {
      scanf("%d", &a);
      a--;
      adj[mat[i][prev_a]].push_back(mat[i][a]);
      prev_a = a;
    }
  }
  rep(i, v) {
    visited[i] = calculated[i] = false;
    dp[i] = 0;
  }
  int ans = 0;
  rep(i, v) {
    int res = dfs(i);
    if (res == -1) {
      ans = -1;
      break;
    }
    ans = max(ans, res);
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

static const int MAXN = 1e+3;
static const int MAXV = MAXN * (MAXN - 1) / 2;
int mat[MAXN][MAXN], dp[MAXV];
vector<int> adj[MAXV];
bool visited[MAXV], calculated[MAXV];

// max length of path from s
int dfs(int u) {
  if (visited[u]) {
    if (!calculated[u])
      return -1;
    return dp[u];
  }
  visited[u] = true;
  dp[u] = 1;
  for (int v : adj[u]) {
    int res = dfs(v);
    if (res == -1)
      return -1;
    dp[u] = max(dp[u], res + 1);
  }
  calculated[u] = true;
  return dp[u];
}

int main() {
  int n;
  cin >> n;
  int cnt = 0, v = n * (n - 1) / 2;
  rep(i, n) for (int j = i + 1; j < n; j++) {
    mat[i][j] = mat[j][i] = cnt;
    cnt++;
  }
  int a, prev_a;
  rep(i, n) {
    scanf("%d", &prev_a);
    prev_a--;
    rep(_, n - 2) {
      scanf("%d", &a);
      a--;
      adj[mat[i][prev_a]].push_back(mat[i][a]);
      prev_a = a;
    }
  }
  rep(i, v) {
    visited[i] = calculated[i] = false;
    dp[i] = 0;
  }
  int ans = 0;
  rep(i, v) {
    int res = dfs(i);
    if (res == -1) {
      ans = -1;
      break;
    }
    ans = max(ans, res);
  }
  cout << ans << "\n";
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
ll MOD = 1000000007;
ll INF = 214748364700000000;
long double EPS = 1e-11;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

ll A[1010][1010];
ll p[1010];
bool done[1010];

int main() {
  ll N;
  cin >> N;
  rep(i, N) rep(j, N - 1) {
    cin >> A[i][j];
    A[i][j]--;
  }
  ll ans = 0, cnt = 0;
  rep(i, N * (N - 1) / 2) {
    ans++;
    bool update = 1;
    fill(done, done + 1000, true);
    rep(j, N - 1) FOR(k, j + 1, N) {
      if (A[j][p[j]] == k && A[k][p[k]] == j && done[j] && done[k]) {
        p[j]++;
        p[k]++;
        done[j] = 0;
        done[k] = 0;
        cnt++;
        update = 0;
      }
    }
    /*rep(j,N) cout << p[j] << ' ';
    cout << endl;*/
    if (i > 50000) {
      cout << N * (N - 1) / 2 << endl;
      return 0;
    }
    if (cnt == N * (N - 1) / 2)
      break;
    if (update) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
ll MOD = 1000000007;
ll INF = 214748364700000000;
long double EPS = 1e-11;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

ll A[1010][1010];
ll p[1010];
bool done[1010];

int main() {
  ll N;
  cin >> N;
  rep(i, N) rep(j, N - 1) {
    cin >> A[i][j];
    A[i][j]--;
  }
  ll ans = 0, cnt = 0;
  rep(i, N * (N - 1) / 2) {
    ans++;
    bool update = 1;
    fill(done, done + 1000, true);
    rep(j, N - 1) FOR(k, j + 1, N) {
      if (A[j][p[j]] == k && A[k][p[k]] == j && done[j] && done[k]) {
        p[j]++;
        p[k]++;
        done[j] = 0;
        done[k] = 0;
        cnt++;
        update = 0;
      }
    }
    /*rep(j,N) cout << p[j] << ' ';
    cout << endl;*/
    if (i > 4000) {
      cout << N * (N - 1) / 2 << endl;
      return 0;
    }
    if (cnt == N * (N - 1) / 2)
      break;
    if (update) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  vector<queue<int>> q(n + 1);
  int a;
  REP(i, n) {
    REP(j, n - 1) {
      cin >> a;
      q[i + 1].push(a);
    }
  }
  VI t(n + 1, 0);
  int i, flaga, flagb;
  for (i = 0; 1; i++) {
    flaga = 0;
    flagb = 0;
    for (int j = 1; j <= n; j++) {
      if (!q[j].empty()) {
        flaga = 1;
        if (t[j] != 1 && t[q[j].front()] != 1) {
          if (q[q[j].front()].front() == j) {
            // cout << j << ' ' << q[j].front() << ' ' << i << '\n';
            flagb = 1;
            t[j] = 1;
            t[q[j].front()] = 1;
            q[q[j].front()].pop();
            q[j].pop();
          }
        }
      }
    }
    REP(i, n)
    t[i + 1] = 0;
    if (flaga == 0)
      break;
    else if (flagb == 0) {
      i = -1;
      break;
    }
  }
  cout << i;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  vector<queue<int>> q(n + 1);
  int a;
  REP(i, n) {
    REP(j, n - 1) {
      cin >> a;
      q[i + 1].push(a);
    }
  }
  VI t(n + 1, 0);
  int i, flaga, flagb;
  for (i = 0; 1; i++) {
    flaga = 0;
    flagb = 0;
    for (int j = n; j >= 1; j--) {
      if (!q[j].empty()) {
        flaga = 1;
        if (t[j] != 1 && t[q[j].front()] != 1) {
          if (q[q[j].front()].front() == j) {
            // cout << j << ' ' << q[j].front() << ' ' << i << '\n';
            flagb = 1;
            t[j] = 1;
            t[q[j].front()] = 1;
            q[q[j].front()].pop();
            q[j].pop();
          }
        }
      }
    }
    REP(i, n)
    t[i + 1] = 0;
    if (flaga == 0)
      break;
    else if (flagb == 0) {
      i = -1;
      break;
    }
  }
  cout << i;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
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) {
      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;
    for (P p : prevQ) {
      int i = p.first, j = p.second;
      a[i].pop_back();
      a[j].pop_back();
    }
    for (P p : prevQ) {
      int i = p.first, j = p.second;
      check(i);
      check(j);
    }
  }
  rep(i, n) {
    if (a[i].size() != 0) {
      puts("-1");
      return 0;
    }
  }
  cout << day << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
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) {
      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, j = p.second;
      a[i].pop_back();
      a[j].pop_back();
    }
    for (P p : prevQ) {
      int i = p.first, j = p.second;
      check(i);
      check(j);
    }
  }
  rep(i, n) {
    if (a[i].size() != 0) {
      puts("-1");
      return 0;
    }
  }
  cout << day << endl;
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  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> cnt(N, 0);
  int ans = 0;
  time_t s, t;
  time(&s);
  while (1) {
    ans++;
    int flag2 = 0;
    time(&t);
    if (t - s > 1) {
      cout << N * (N - 1) / 2 << endl;
      return 0;
    }
    vector<int> used(N, 0);
    for (int i = 0; i < N; i++) {
      int ind = A[i][cnt[i]];
      if (used[i] == 1 || used[ind] == 1 || cnt[i] == N - 1)
        continue;
      if (A[ind][cnt[ind]] == i) {
        cnt[i]++;
        cnt[ind]++;
        used[i] = 1;
        used[ind] = 1;
        flag2 = 1;
      }
    }
    if (flag2 == 0) {
      cout << -1 << endl;
      return 0;
    }
    int flag = 0;
    for (int i = 0; i < N; i++) {
      if (cnt[i] != N - 1)
        flag = 1;
    }
    if (flag == 0)
      break;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  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> cnt(N, 0);
  int ans = 0;
  time_t s, t;
  time(&s);
  while (1) {
    ans++;
    int flag2 = 0;
    time(&t);
    if (t - s > 0) {
      cout << N * (N - 1) / 2 << endl;
      return 0;
    }
    vector<int> used(N, 0);
    for (int i = 0; i < N; i++) {
      int ind = A[i][cnt[i]];
      if (used[i] == 1 || used[ind] == 1 || cnt[i] == N - 1)
        continue;
      if (A[ind][cnt[ind]] == i) {
        cnt[i]++;
        cnt[ind]++;
        used[i] = 1;
        used[ind] = 1;
        flag2 = 1;
      }
    }
    if (flag2 == 0) {
      cout << -1 << endl;
      return 0;
    }
    int flag = 0;
    for (int i = 0; i < N; i++) {
      if (cnt[i] != N - 1)
        flag = 1;
    }
    if (flag == 0)
      break;
  }
  cout << ans << endl;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
queue<int> a[1001];
int main() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      int x;
      cin >> x;
      a[i].push(x);
    }
  }
  int sc = n * (n - 1) / 2;
  // cout<<sc<<endl;
  int cnt = 0;
  while (sc > 0) {
    // cout<<sc<<endl;
    vector<int> temp;
    for (int i = 1; i <= n; i++) {
      // int t=a[i].front();
      if (a[a[i].front()].front() == i && i < a[i].front()) {

        // cout<<i<<" "<<a[i][0]<<endl;
        temp.push_back(i);
        temp.push_back(a[i].front());
      }
    }
    if (temp.size() == 0) {
      cout << -1;
      return 0;
    }
    // cout<<"fdf"<<endl;
    cnt++;
    for (int i = 0; i < temp.size(); i++) {
      a[temp[i]].pop();
    }
    sc -= (temp.size() / 2);
  }
  cout << cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
queue<int> a[1001];
int main() {
  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      int x;
      cin >> x;
      a[i].push(x);
    }
  }
  int sc = n * (n - 1) / 2;
  // cout<<sc<<endl;
  int cnt = 0;
  while (sc > 0) {
    // cout<<sc<<endl;
    vector<int> temp;
    for (int i = 1; i <= n; i++) {
      // int t=a[i].front();
      if (a[a[i].front()].front() == i && i < a[i].front()) {

        // cout<<i<<" "<<a[i][0]<<endl;
        temp.push_back(i);
        temp.push_back(a[i].front());
        continue;
      }
    }
    if (temp.size() == 0) {
      cout << -1;
      return 0;
    }
    // cout<<"fdf"<<endl;
    cnt++;
    for (int i = 0; i < temp.size(); i++) {
      a[temp[i]].pop();
    }
    sc -= (temp.size() / 2);
  }
  cout << cnt << endl;
}
Problem p02925 (C++)

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

#define INF INT_MAX
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef std::pair<int, int> pii;

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<deque<int>> a(n);
  REP(i, n) {
    REP(j, n - 1) {
      int aa;
      cin >> aa;
      a[i].push_back(aa - 1);
    }
  }

  int ans = 0;
  int aaaaa = n * (n - 1) / 2;
  bool fffff = true;
  for (;;) {
    vector<bool> f(n, false);
    bool comp = true;
    bool all_f = true;
    REP(j, n) {
      if (a[j].empty())
        continue;
      comp = false;

      int enemy = a[j].front();
      int enemy_to_enemy = a[enemy].front();
      if (f[enemy])
        continue;
      if (j != enemy_to_enemy)
        continue;
      f[j] = true;
      f[enemy] = true;
      all_f = false;
      // cout << enemy << " " << enemy_to_enemy << endl;
    }
    if (comp)
      break;

    if (all_f) {
      ans = -1;
      break;
    }
    ans++;
    int c = 0;
    REP(i, n) {
      if (f[i]) {
        a[i].pop_front();
        c++;
      }
    }
    if (ans > 10000000) {
      cout << aaaaa << endl;
      return 0;
    }
    // cout << ans << endl;
  }

  cout << ans << endl;

  return 0;
}

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

#define INF INT_MAX
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef std::pair<int, int> pii;

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<deque<int>> a(n);
  REP(i, n) {
    REP(j, n - 1) {
      int aa;
      cin >> aa;
      a[i].push_back(aa - 1);
    }
  }

  int ans = 0;
  int aaaaa = n * (n - 1) / 2;
  bool fffff = true;
  for (;;) {
    vector<bool> f(n, false);
    bool comp = true;
    bool all_f = true;
    REP(j, n) {
      if (a[j].empty())
        continue;
      comp = false;

      int enemy = a[j].front();
      int enemy_to_enemy = a[enemy].front();
      if (f[enemy])
        continue;
      if (j != enemy_to_enemy)
        continue;
      f[j] = true;
      f[enemy] = true;
      all_f = false;
      // cout << enemy << " " << enemy_to_enemy << endl;
    }
    if (comp)
      break;

    if (all_f) {
      ans = -1;
      break;
    }
    ans++;
    int c = 0;
    REP(i, n) {
      if (f[i]) {
        a[i].pop_front();
        c++;
      }
    }
    if (ans > 100000) {
      cout << aaaaa << endl;
      return 0;
    }
    // cout << ans << endl;
  }

  cout << ans << endl;

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc139/tasks/abc139_e
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define EPS (1e-9)
#define INF (1e9)
#define INFL (1e18)
#define MOD (1000000007)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
// ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, t, cnt = 0;
  cin >> n;
  vector<queue<int>> q(n);
  REP(i, n) REP(j, n - 1) {
    cin >> t;
    q[i].push(t - 1);
  }
  set<pii> st;
  set<int> sl;
  REP(i, n) sl.insert(i);
  while (true) {
    // 全てのキューが空なら終了
    if (sl.size() == 0) {
      break;
    }
    set<int> sk;
    bool nfound = true;
    // 前回更新したペアのみ
    for (auto i : sl) {
      pii p = {i, q[i].front()};
      if (p.first > p.second) {
        swap(p.first, p.second);
      }
      if (st.count(p) == 0) {
        st.insert(p);
      } else {
        nfound = false;
        q[p.first].pop();
        q[p.second].pop();
        sk.insert(p.first);
        sk.insert(p.second);
      }
    }
    if (nfound) {
      cnt = -1;
      break;
    }
    sl = sk;
    cnt++;
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/abc139/tasks/abc139_e
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define EPS (1e-9)
#define INF (1e9)
#define INFL (1e18)
#define MOD (1000000007)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
// ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, t, cnt = 0;
  cin >> n;
  vector<queue<int>> q(n);
  REP(i, n) REP(j, n - 1) {
    cin >> t;
    q[i].push(t - 1);
  }
  set<pii> st;
  set<int> sl;
  REP(i, n) sl.insert(i);
  while (true) {
    // 全てのキューが空なら終了
    if (sl.size() == 0) {
      break;
    }
    set<int> sk;
    bool nfound = true;
    // 前回更新したペアのみ
    for (auto i : sl) {
      pii p = {i, q[i].front()};
      if (p.first > p.second) {
        swap(p.first, p.second);
      }
      if (st.count(p) == 0) {
        st.insert(p);
      } else {
        nfound = false;
        q[p.first].pop();
        q[p.second].pop();
        if (!q[p.first].empty()) {
          sk.insert(p.first);
        }
        if (!q[p.second].empty()) {
          sk.insert(p.second);
        }
      }
    }
    if (nfound) {
      cnt = -1;
      break;
    }
    sl = sk;
    cnt++;
  }
  cout << cnt << endl;

  return 0;
}
Problem p02925 (C++)

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

const int MAX_N = 1e3;
vector<vector<int>> G((MAX_N + 1) * (MAX_N + 1));
vector<int> dis(MAX_N, -1);
vector<bool> start(MAX_N, false);
vector<bool> done(MAX_N, false);

int dfs(int obj) {
  if (start[obj]) {
    if (!done[obj])
      return -1;
    return dis[obj];
  } else {
    start[obj] = true;
    int k = 0;
    for (int x : G[obj]) {
      int l = dfs(x);
      if (l == -1)
        return -1;
      else
        k = max(k, l);
    }
    done[obj] = true;
    dis[obj] = k + 1;
    return dis[obj];
  }
}

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    int k = (max(a, i) - 1) * (max(a, i) - 2) / 2 + min(a, i);
    for (int j = 0; j < N - 2; j++) {
      int a;
      cin >> a;
      int l = (max(a, i) - 1) * (max(a, i) - 2) / 2 + min(a, i);
      G[k].push_back(l);
      k = l;
    }
  }
  int k = 0;
  for (int i = 1; i <= N * (N - 1) / 2; i++) {
    if (dfs(i) == -1) {
      k = -1;
      break;
    } else {
      k = max(k, dfs(i));
    }
  }
  cout << k << endl;
}


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

const int MAX_N = 1000;
vector<vector<int>> G(MAX_N *(MAX_N - 1) / 2 + 1);
vector<int> dis(MAX_N *(MAX_N - 1) / 2 + 1, -1);
vector<bool> start(MAX_N *(MAX_N - 1) / 2 + 1, false);
vector<bool> done(MAX_N *(MAX_N - 1) / 2 + 1, false);

int dfs(int obj) {
  if (start[obj]) {
    if (!done[obj])
      return -1;
    return dis[obj];
  } else {
    start[obj] = true;
    int k = 0;
    for (int x : G[obj]) {
      int l = dfs(x);
      if (l == -1)
        return -1;
      else
        k = max(k, l);
    }
    done[obj] = true;
    dis[obj] = k + 1;
    return dis[obj];
  }
}

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    int k = (max(a, i) - 1) * (max(a, i) - 2) / 2 + min(a, i);
    for (int j = 0; j < N - 2; j++) {
      int a;
      cin >> a;
      int l = (max(a, i) - 1) * (max(a, i) - 2) / 2 + min(a, i);
      G[k].push_back(l);
      k = l;
    }
  }
  int k = 0;
  for (int i = 1; i <= N * (N - 1) / 2; i++) {
    if (dfs(i) == -1) {
      k = -1;
      break;
    } else {
      k = max(k, dfs(i));
    }
  }
  cout << k << endl;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;

bool seen[2000000]{};
bool finished[2000000]{};
int d[2000000]{};
int dfs(const Graph &G, int now) {
  if (seen[now] == true)
    return d[now];
  seen[now] = true;
  for (auto e : G[now]) {
    if (seen[e] && !finished[e]) {
      cout << -1 << endl;
      exit(0);
    }
    d[now] = max(d[now], dfs(G, e) + 1);
  }
  finished[now] = true;
  return d[now];
}

int encode(int i, int j) {
  if (i < j)
    swap(i, j);
  return 1000 * i + j;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  Graph G(N * (N - 1) / 2 + 1);
  rep(i, N) {
    int now = 0;
    rep(j, N - 1) {
      int k;
      cin >> k;
      --k;
      int next = encode(i, k);
      G[now].push_back(next);
      now = next;
    }
  }
  cout << dfs(G, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;

bool seen[2000000]{};
bool finished[2000000]{};
int d[2000000]{};
int dfs(const Graph &G, int now) {
  if (seen[now] == true)
    return d[now];
  seen[now] = true;
  for (auto e : G[now]) {
    if (seen[e] && !finished[e]) {
      cout << -1 << endl;
      exit(0);
    }
    d[now] = max(d[now], dfs(G, e) + 1);
  }
  finished[now] = true;
  return d[now];
}

int encode(int i, int j) {
  if (i < j)
    swap(i, j);
  return 1000 * i + j;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  Graph G(2000000);
  rep(i, N) {
    int now = 0;
    rep(j, N - 1) {
      int k;
      cin >> k;
      --k;
      int next = encode(i, k);
      G[now].push_back(next);
      now = next;
    }
  }
  cout << dfs(G, 0) << endl;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int M = 1001;
int n, A[M][M];
int id[M][M], tot;
vector<int> d[500001];
int dp[M], ans;
bool fl;
int dfs(int x) {
  if (dp[x])
    return dp[x];
  int res = 0;
  for (int i = 0; i < (int)d[x].size(); i++) {
    int y = d[x][i];
    res = max(res, dfs(y));
  }
  dp[x] = res + 1;
  return dp[x];
}
int dfn[500001], ToT, low[500001], ins[500001], stk[500001], top;
void Tarjan(int x) {
  dfn[x] = low[x] = ++ToT;
  ins[x] = 1;
  stk[++top] = x;
  for (int i = 0; i < (int)d[x].size(); i++) {
    int y = d[x][i];
    if (!dfn[y]) {
      Tarjan(y);
      low[x] = min(low[x], low[y]);
    } else if (ins[y])
      low[x] = min(low[x], dfn[y]);
  }
  if (dfn[x] == low[x]) {
    int e;
    int cnt = 0;
    do {
      e = stk[top--];
      ins[e] = 0;
      cnt++;
    } while (e != x);
    if (cnt > 1)
      fl = 1;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++)
      scanf("%d", &A[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++)
      id[i][j] = id[j][i] = ++tot;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 2; j < n; j++)
      d[id[i][A[i][j - 1]]].push_back(id[i][A[i][j]]);
  for (int i = 1; i <= tot; i++)
    if (!dfn[i])
      Tarjan(i);
  if (fl) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= tot; i++)
    ans = max(ans, dfs(i));
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int M = 1001;
int n, A[M][M];
int id[M][M], tot;
vector<int> d[500001];
int dp[500001], ans;
bool fl;
int dfs(int x) {
  if (dp[x])
    return dp[x];
  int res = 0;
  for (int i = 0; i < (int)d[x].size(); i++) {
    int y = d[x][i];
    res = max(res, dfs(y));
  }
  dp[x] = res + 1;
  return dp[x];
}
int dfn[500001], ToT, low[500001], ins[500001], stk[500001], top;
void Tarjan(int x) {
  dfn[x] = low[x] = ++ToT;
  ins[x] = 1;
  stk[++top] = x;
  for (int i = 0; i < (int)d[x].size(); i++) {
    int y = d[x][i];
    if (!dfn[y]) {
      Tarjan(y);
      low[x] = min(low[x], low[y]);
    } else if (ins[y])
      low[x] = min(low[x], dfn[y]);
  }
  if (dfn[x] == low[x]) {
    int e;
    int cnt = 0;
    do {
      e = stk[top--];
      ins[e] = 0;
      cnt++;
    } while (e != x);
    if (cnt > 1)
      fl = 1;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++)
      scanf("%d", &A[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++)
      id[i][j] = id[j][i] = ++tot;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 2; j < n; j++)
      d[id[i][A[i][j - 1]]].push_back(id[i][A[i][j]]);
  for (int i = 1; i <= tot; i++)
    if (!dfn[i])
      Tarjan(i);
  if (fl) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= tot; i++)
    ans = max(ans, dfs(i));
  printf("%d\n", ans);
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define mod 1000000007;

int main() {
  int n, a[1010][1010], now[1010];
  cin >> n;
  ll hoge = 0, en = (n * (n - 1)) / 2;
  for (int i = 0; i < n; i++)
    now[i] = n - 2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
    }
  }
  int ans = 0;
  bool flag[1010], cc[1010];
  int last[1010];
  for (int k = 0; k < n; k++)
    cc[k] = 0;
  for (int k = 0; k < n; k++)
    last[k] = -1;
  bool ff = 1;
  while (1) {
    ff = 1;
    for (int i = 0; i < n; i++) {
      if (last[i] == ans || cc[i])
        continue;
      for (int j = i + 1; j < n; j++) {
        if (last[j] == ans || cc[j])
          continue;
        if (a[i][now[i]] == (j + 1) && a[j][now[j]] == (i + 1) &&
            last[i] != ans && last[j] != ans) {
          last[i] = ans;
          last[j] = ans;
          now[i]--;
          now[j]--;
          hoge++;
          ff = 0;
          if (now[i] == -1)
            cc[i] = 1;
          if (now[j] == -1)
            cc[j] = 1;
        }
      }
    }
    ans++;
    if (hoge == en) {
      cout << ans << endl;
      return 0;
    }
    if (ff)
      break;
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define mod 1000000007;

int main() {
  int n, a[1010][1010], now[1010];
  cin >> n;
  ll hoge = 0, en = (n * (n - 1)) / 2;
  for (int i = 0; i < n; i++)
    now[i] = n - 2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> a[i][j];
    }
  }
  int ans = 0;
  bool flag[1010], cc[1010];
  int last[1010];
  for (int k = 0; k < n; k++)
    cc[k] = 0;
  for (int k = 0; k < n; k++)
    last[k] = -1;
  bool ff = 1;
  while (1) {
    ff = 1;
    for (int i = 0; i < n; i++) {
      if (last[i] == ans || cc[i])
        continue;
      int j = a[i][now[i]] - 1;
      if (a[i][now[i]] == (j + 1) && a[j][now[j]] == (i + 1) &&
          last[i] != ans && last[j] != ans) {
        last[i] = ans;
        last[j] = ans;
        now[i]--;
        now[j]--;
        hoge++;
        ff = 0;
        if (now[i] == -1)
          cc[i] = 1;
        if (now[j] == -1)
          cc[j] = 1;
      }
    }
    ans++;
    if (hoge == en) {
      cout << ans << endl;
      return 0;
    }
    if (ff)
      break;
  }
  cout << -1 << endl;
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  int x;
  vector<queue<int>> b(n);
  rep(i, n) rep(j, n - 1) {
    cin >> x;
    x--;
    b[i].emplace(x);
  }
  set<int> seen;
  int ans = -1;
  bool f = 1;
  while (f) {
    f = 0;
    rep(i, n) {
      if (b[i].empty())
        continue;
      int op = b[i].front();
      if (seen.count(op) || seen.count(i))
        continue;
      if (b[op].front() != i)
        continue;
      f = 1;
      b[i].pop();
      b[op].pop();
      seen.insert(op);
      seen.insert(i);
    }
    ans++;
    seen.clear();
  }
  rep(i, n) {
    if (!b[i].empty()) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
} /*
 ./problem.exe
 */

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  int x;
  vector<queue<int>> b(n);
  rep(i, n) rep(j, n - 1) {
    cin >> x;
    x--;
    b[i].emplace(x);
  }
  set<int> seen;
  int ans = -1;
  bool f = 1;
  while (f) {
    f = 0;
    rep(i, n) {
      if (b[i].empty())
        continue;
      int op = b[i].front();
      if (op < i)
        continue;
      if (seen.count(op) || seen.count(i))
        continue;
      if (b[op].front() != i)
        continue;
      f = 1;
      b[i].pop();
      b[op].pop();
      seen.insert(op);
      seen.insert(i);
    }
    ans++;
    seen.clear();
  }
  rep(i, n) {
    if (!b[i].empty()) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
} /*
 ./problem.exe
 */
Problem p02925 (C++)

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

const bool DEBUG = false;

int main(int argc, char const *argv[]) {
  int N;
  cin >> N;
  Table A(N, vector<int>(N - 1));
  for (size_t i = 0; i < N; i++) {
    for (size_t j = 0; j < N - 1; j++) {
      cin >> A[i][j];
    }
  }

  if (DEBUG) {
    for (size_t i = 0; i < N; i++) {
      for (size_t j = 0; j < N - 1; j++) {
        cout << A[i][j] << ", ";
      }
      cout << endl;
    }
  }

  int total = N * (N - 1);
  int days = 0;
  bool not_finished = true;
  vector<int> matched;
  while (not_finished) {
    bool found = false;
    matched.clear();
    for (size_t i = 0; i < N; i++) {
      // if (matched.find(i) != matched.end()) continue;
      if (A[i].size() == 0)
        continue;
      int opponent = A[i][0] - 1;
      if (DEBUG)
        cout << i + 1 << " vs " << opponent + 1 << endl;
      if (A[opponent].size() == 0) {
        cout << "-1" << endl;
        return 0;
      }
      if (i == A[opponent][0] - 1) {
        if (DEBUG)
          cout << "found" << endl;
        found = true;
        matched.push_back(i);
        // matched.insert(i);
        // matched.insert(opponent);
      }
    }
    total -= matched.size();
    for (auto &&player : matched)
      A[player].erase(A[player].begin());

    if (!found) {
      cout << "-1" << endl;
      return 0;
    }
    if (DEBUG)
      cout << "total: " << total << endl;
    not_finished = total > 0;
    days++;
  }

  cout << days << endl;

  return 0;
}


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

const bool DEBUG = false;

int main(int argc, char const *argv[]) {
  int N;
  cin >> N;
  Table A(N, vector<int>(N - 1));
  for (size_t i = 0; i < N; i++) {
    for (size_t j = 0; j < N - 1; j++) {
      cin >> A[i][j];
    }
  }

  if (DEBUG) {
    for (size_t i = 0; i < N; i++) {
      for (size_t j = 0; j < N - 1; j++) {
        cout << A[i][j] << ", ";
      }
      cout << endl;
    }
  }

  int total = N * (N - 1);
  int days = 0;
  bool not_finished = true;
  vector<int> matched;
  while (not_finished) {
    bool found = false;
    matched.clear();
    for (size_t i = 0; i < N; i++) {
      // if (matched.find(i) != matched.end()) continue;
      if (A[i].size() == 0)
        continue;
      int opponent = A[i][0] - 1;
      if (DEBUG)
        cout << i + 1 << " vs " << opponent + 1 << endl;
      // if (A[opponent].size() == 0) {
      //   cout << "-1" << endl;
      //   return 0;
      // }
      if (i == A[opponent][0] - 1) {
        if (DEBUG)
          cout << "found" << endl;
        found = true;
        matched.push_back(i);
        // matched.insert(i);
        // matched.insert(opponent);
      }
    }
    total -= matched.size();
    for (auto &&player : matched)
      A[player].erase(A[player].begin());

    if (!found) {
      cout << "-1" << endl;
      return 0;
    }
    if (DEBUG)
      cout << "total: " << total << endl;
    not_finished = total > 0;
    days++;
  }

  cout << days << endl;

  return 0;
}