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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
using namespace std;
const int N = 1005;
void fast() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int n, a[N][N];
queue<int> q[N];
vi v, e;
bool check() {
  for (int i = 0; i < n; i++) {
    if (!q[i].empty()) {
      return false;
    }
  }
  return true;
}
int main() {
  fast();
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n - 1; j++)
      cin >> a[i][j], q[i].push(a[i][j] - 1);
  for (int i = 0; i < n; i++)
    v.pb(i);
  for (int ans = 1;; ans++) {
    e.clear();
    vector<bool> use(n);
    if (check()) {
      cout << ans - 1 << endl;
      return 0;
    }
    bool ok = false;
    for (int k = 0; k < v.size(); k++) {
      int i = v[i];
      if (!use[i] && !q[i].empty()) {
        int nxt = q[i].front();
        if (!use[nxt] && !q[nxt].empty() && q[nxt].front() == i) {
          ok = true;
          use[nxt] = true;
          use[i] = true;
          q[nxt].pop();
          q[i].pop();
          e.pb(nxt);
          e.pb(i);
        }
      }
    }
    if (!ok) {
      cout << "-1";
      return 0;
    }
    v = e;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
using namespace std;
const int N = 1005;
void fast() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int n, a[N][N];
queue<int> q[N];
vi v, e;
bool check() {
  for (int i = 0; i < n; i++) {
    if (!q[i].empty()) {
      return false;
    }
  }
  return true;
}
int main() {
  fast();
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n - 1; j++)
      cin >> a[i][j], q[i].push(a[i][j] - 1);
  for (int i = 0; i < n; i++)
    v.pb(i);
  for (int ans = 1;; ans++) {
    e.clear();
    vector<bool> use(n);
    if (check()) {
      cout << ans - 1 << endl;
      return 0;
    }
    bool ok = false;
    for (int k = 0; k < v.size(); k++) {
      int i = v[k];
      if (!use[i] && !q[i].empty()) {
        int nxt = q[i].front();
        if (!use[nxt] && !q[nxt].empty() && q[nxt].front() == i) {
          ok = true;
          use[nxt] = true;
          use[i] = true;
          q[nxt].pop();
          q[i].pop();
          e.pb(nxt);
          e.pb(i);
        }
      }
    }
    if (!ok) {
      cout << "-1";
      return 0;
    }
    v = e;
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(I, A, B) for (int I = int(A); I < int(B); I++)

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

  clock_t start = clock();

  int N, ans = 0;
  cin >> N;
  vector<int> A[N + 1];
  FOR(i, 1, N + 1) {
    A[i].resize(N);
    FOR(j, 1, N) { cin >> A[i][j]; }
  }

  bool game = true;
  vector<int> ind(N + 1, 1); // A[i][ind[i]]
  vector<int> nextOPP(N + 1, -1);
  vector<int> rest;
  FOR(i, 1, N + 1) rest.push_back(i);
  while (game) {
    game = false;
    ans++;
    vector<int> restt;
    for (int i : rest)
      nextOPP[i] = -1;
    for (int i : rest) {
      if (ind[i] >= N)
        continue;
      nextOPP[i] = A[i][ind[i]];
      if (nextOPP[nextOPP[i]] == i) {
        game = true;
        ind[i]++;
        ind[nextOPP[i]]++;
      }
    }
    for (int i : rest)
      if (ind[i] < N)
        restt.push_back(i);
    rest = restt;

    clock_t end = clock();
    double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
    if (time >= 1800.0) {
      cout << N * (N - 1) / 2 << endl;
    }
  }
  bool allok = true;
  FOR(i, 1, N + 1) if (ind[i] < N) allok = false;
  if (allok) {
    cout << ans - 1 << endl;
  } else {
    cout << -1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(I, A, B) for (int I = int(A); I < int(B); I++)

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

  clock_t start = clock();

  int N, ans = 0;
  cin >> N;
  vector<int> A[N + 1];
  FOR(i, 1, N + 1) {
    A[i].resize(N);
    FOR(j, 1, N) { cin >> A[i][j]; }
  }

  bool game = true;
  vector<int> ind(N + 1, 1); // A[i][ind[i]]
  vector<int> nextOPP(N + 1, -1);
  vector<int> rest;
  FOR(i, 1, N + 1) rest.push_back(i);
  while (game) {
    game = false;
    ans++;
    vector<int> restt;
    for (int i : rest)
      nextOPP[i] = -1;
    for (int i : rest) {
      if (ind[i] >= N)
        continue;
      nextOPP[i] = A[i][ind[i]];
      if (nextOPP[nextOPP[i]] == i) {
        game = true;
        ind[i]++;
        ind[nextOPP[i]]++;
      }
    }
    for (int i : rest)
      if (ind[i] < N)
        restt.push_back(i);
    rest = restt;

    clock_t end = clock();
    double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
    if (time >= 1800.0) {
      cout << N * (N - 1) / 2 << endl;
      return 0;
    }
  }
  bool allok = true;
  FOR(i, 1, N + 1) if (ind[i] < N) allok = false;
  if (allok) {
    cout << ans - 1 << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p02925 (C++)

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

using namespace std;

const int N = 1005;

int n, a[N][N], ans = 0, nxt[N], ptr[N];

int readInt() {
  char c;
  int ans = 0;
  while (1) {
    c = getchar();
    if (c == ' ' || c == '\n')
      return ans;
    ans = (ans << 3) + (ans << 1) + (c - '0');
  }
}

int main() {
  int n;
  n = readInt();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; j++) {
      a[i][j] = readInt();
    }

    ptr[i] = 1;
  }

  while (1) {
    bool ok = true;
    // if (ans >= 50000) return !printf("%d", -1);
    for (int i = 1; i <= n; i++) {
      if (ptr[i] <= n - 1) {
        ok = false;
        break;
      }
    }

    if (ok)
      break;
    ans++;
    memset(nxt, 0, sizeof(nxt));
    // for (int i = 1; i <= n; i++) ptr2[i] = ptr[i];

    ok = true;
    for (int i = 1; i <= n; i++) {
      if (ptr[i] >= n)
        continue;
      int cur = a[i][ptr[i]];

      // cout << i << " " << cur << " " << ans << "\n";
      // if (nxt[cur] && nxt[cur] != i) continue;
      // if (nxt[i]) continue;
      if (nxt[i] || nxt[cur]) {
        // ptr[cur]++;
        continue;
      }

      if (ptr[cur] >= n)
        continue;
      if (a[cur][ptr[cur]] != i)
        continue;
      nxt[cur] = i;
      nxt[i] = cur;
      ptr[i]++;
      ptr[cur]++;
      ok = false;
    }

    if (ok)
      return !printf("%d", -1);
  }

  printf("%d", ans);
  return 0;
}


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

using namespace std;

const int N = 1005;

int n, a[N][N], ans = 0, nxt[N], ptr[N];

int readInt() {
  char c;
  int ans = 0;
  while (1) {
    c = getchar();
    if (c == ' ' || c == '\n')
      return ans;
    ans = (ans << 3) + (ans << 1) + (c - '0');
  }
}

int main() {
  int n;
  n = readInt();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; j++) {
      a[i][j] = readInt();
    }

    ptr[i] = 1;
  }

  while (1) {
    bool ok = true;
    if (ans >= 50000)
      return !printf("%d", n * (n - 1) / 2);
    for (int i = 1; i <= n; i++) {
      if (ptr[i] <= n - 1) {
        ok = false;
        break;
      }
    }

    if (ok)
      break;
    ans++;
    memset(nxt, 0, sizeof(nxt));
    // for (int i = 1; i <= n; i++) ptr2[i] = ptr[i];

    ok = true;
    for (int i = 1; i <= n; i++) {
      if (ptr[i] >= n)
        continue;
      int cur = a[i][ptr[i]];

      // cout << i << " " << cur << " " << ans << "\n";
      // if (nxt[cur] && nxt[cur] != i) continue;
      // if (nxt[i]) continue;
      if (nxt[i] || nxt[cur]) {
        // ptr[cur]++;
        continue;
      }

      if (ptr[cur] >= n)
        continue;
      if (a[cur][ptr[cur]] != i)
        continue;
      nxt[cur] = i;
      nxt[i] = cur;
      ptr[i]++;
      ptr[cur]++;
      ok = false;
    }

    if (ok)
      return !printf("%d", -1);
  }

  printf("%d", ans);
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
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++) {
      int b;
      cin >> b;
      a.at(i).at(j) = b - 1;
    }
  }

  double start_time = clock();

  vector<int> game(n);
  int game_num = 0;
  int max_game_num = n * (n - 1) / 2;
  int answer = 0;

  while (true) {

    bool played_one_game = false;

    vector<int> aite(n);

    for (int i = 0; i < n; i++) {
      if (game.at(i) == n - 1) {
        aite.at(i) = -1;
        continue;
      }
      aite.at(i) = a.at(i).at(game.at(i));
    }

    for (int i = 0; i < n; i++) {
      int teki = aite.at(i);
      if (teki == -1)
        continue;
      if (aite.at(teki) == i && i < teki) {
        game.at(i)++;
        game.at(teki)++;
        game_num++;
        played_one_game = true;
      }
    }

    if (!played_one_game) {
      answer = -1;
      break;
    }

    answer++;

    bool finished = true;
    for (int i = 0; i < n; i++) {
      if (game.at(i) != n - 1)
        finished = false;
    }

    if (finished)
      break;

    double now_time = clock();
    if ((now_time - start_time) / CLOCKS_PER_SEC >= 1.8) {
      answer = n * (n - 1) / 2;
      break;
    }
  }

  cout << answer << endl;
}


Fixed submission:
#include <bits/stdc++.h>
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++) {
      int b;
      cin >> b;
      a.at(i).at(j) = b - 1;
    }
  }

  double start_time = clock();

  vector<int> game(n);
  int game_num = 0;
  int max_game_num = n * (n - 1) / 2;
  int answer = 0;

  while (true) {

    bool played_one_game = false;

    vector<int> aite(n);

    for (int i = 0; i < n; i++) {
      if (game.at(i) == n - 1) {
        aite.at(i) = -1;
        continue;
      }
      aite.at(i) = a.at(i).at(game.at(i));
    }

    for (int i = 0; i < n; i++) {
      int teki = aite.at(i);
      if (teki == -1)
        continue;
      if (aite.at(teki) == i && i < teki) {
        game.at(i)++;
        game.at(teki)++;
        game_num++;
        played_one_game = true;
      }
    }

    if (!played_one_game) {
      answer = -1;
      break;
    }

    answer++;

    bool finished = true;
    for (int i = 0; i < n; i++) {
      if (game.at(i) != n - 1)
        finished = false;
    }

    if (finished)
      break;

    double now_time = clock();
    if ((now_time - start_time) / CLOCKS_PER_SEC >= 1.5) {
      answer = n * (n - 1) / 2;
      break;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
// const int INF = (1 << 30) - 1;
// const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;

int N;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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 tmp;
      cin >> tmp;
      tmp--;
      A[i][j] = tmp;
    }
  }

  int ans = 0;
  int done = 0;
  vector<int> cur(N, 0);
  unordered_map<int, int> mp;
  while (done != N) {
    bool proceed = false;
    for (int i = 0; i < N; i++) {
      if (mp[i] == (ans + 1) || cur[i] == (N - 1)) {
        continue;
      }
      int a = A[i][cur[i]];
      if (mp[a] == (ans + 1) || cur[a] == (N - 1)) {
        continue;
      }
      int b = A[a][cur[a]];
      if (b == i) {
        proceed = true;
        cur[i]++;
        cur[a]++;
        mp[i] = (ans + 1);
        mp[a] = (ans + 1);
        if (cur[i] == (N - 1)) {
          done++;
        }
        if (cur[a] == (N - 1)) {
          done++;
        }
      }
    }
    if (!proceed) {
      cout << -1 << endl;
      return 0;
    }
    ans++;
    if (ans > 50000) {
      cout << 499500 << endl;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
// const int INF = (1 << 30) - 1;
// const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;

int N;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  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 tmp;
      cin >> tmp;
      tmp--;
      A[i][j] = tmp;
    }
  }

  int ans = 0;
  int done = 0;
  vector<int> cur(N, 0);
  unordered_map<int, int> mp;
  while (done != N) {
    bool proceed = false;
    for (int i = 0; i < N; i++) {
      if (mp[i] == (ans + 1) || cur[i] == (N - 1)) {
        continue;
      }
      int a = A[i][cur[i]];
      if (mp[a] == (ans + 1) || cur[a] == (N - 1)) {
        continue;
      }
      int b = A[a][cur[a]];
      if (b == i) {
        proceed = true;
        cur[i]++;
        cur[a]++;
        mp[i] = (ans + 1);
        mp[a] = (ans + 1);
        if (cur[i] == (N - 1)) {
          done++;
        }
        if (cur[a] == (N - 1)) {
          done++;
        }
      }
    }
    if (!proceed) {
      cout << -1 << endl;
      return 0;
    }
    ans++;
    if (ans > 50000) {
      cout << 499500 << endl;
      return 0;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
int n, m, i, j, k, a, b;
int str[1005][1005], vis[1050], top[1050];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    for (j = 1; j < n; j++) {
      scanf("%d", &str[i][j]);
    }
  }
  for (i = 1; i <= n; i++) {
    top[i] = 1;
  }
  int day = 1;
  while (1) {
    bool con = false;
    for (i = 1; i <= n; i++) {
      if (vis[i] == day || top[i] >= n)
        continue;
      int v = str[i][top[i]];
      if (vis[v] != day && str[v][top[v]] == i) {
        top[v]++;
        top[i]++;
        vis[v] = day;
        vis[i] = day;
        con = true;
      }
    }
    if (!con)
      break;
    day++;
  }
  for (i = 1; i <= n; i++) {
    if (top[i] != n)
      break;
  }
  if (i != n + 1)
    printf("-1\n");
  else
    printf("%d\n", day - 1);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
int n, m, i, j, k, a, b;
int str[1050][1050], vis[1050], top[1050];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    for (j = 1; j < n; j++) {
      scanf("%d", &str[i][j]);
    }
  }
  for (i = 1; i <= n; i++) {
    top[i] = 1;
  }
  int day = 1;
  while (1) {
    bool con = false;
    for (i = 1; i <= n; i++) {
      if (vis[i] == day || top[i] >= n)
        continue;
      int v = str[i][top[i]];
      if (vis[v] != day && str[v][top[v]] == i) {
        top[v]++;
        top[i]++;
        vis[v] = day;
        vis[i] = day;
        con = true;
      }
    }
    if (!con)
      break;
    day++;
  }
  for (i = 1; i <= n; i++) {
    if (top[i] != n)
      break;
  }
  if (i != n + 1)
    printf("-1\n");
  else
    printf("%d\n", day - 1);
  return 0;
}
Problem p02925 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int main() {
  int n;
  cin >> n;
  vector<queue<int>> A(n);
  int a;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      cin >> a;
      A[i].push(a - 1);
    }
  }

  vector<int> v1, v2;
  for (int i = 0; i < n; ++i) {
    v1.push_back(i);
  }

  int res = 0;
  while (1) {
    bool fin = true;
    bool loop = true;
    vector<bool> checked(n, false);
    for (auto i : v1) {
      if (checked[i])
        continue;

      if (A[i].empty()) {
        continue;
      } else {
        fin = false;
      }

      if (A[A[i].front()].front() == i && !checked[A[i].front()]) {
        checked[i] = true;
        checked[A[i].front()] = true;
        v2.push_back(i);
        v2.push_back(A[i].front());

        A[A[i].front()].pop();
        A[i].pop();
        loop = false;
      }
    }
    if (fin)
      break;
    if (loop) {
      cout << -1 << endl;
      return 0;
    }
    v1.swap(v2);
    res++;
  }
  cout << res << endl;
}


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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int main() {
  int n;
  cin >> n;
  vector<queue<int>> A(n);
  int a;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      cin >> a;
      A[i].push(a - 1);
    }
  }

  vector<int> v1, v2;
  for (int i = 0; i < n; ++i) {
    v1.push_back(i);
  }

  int res = 0;
  while (1) {
    bool fin = true;
    bool loop = true;
    vector<bool> checked(n, false);
    for (auto i : v1) {
      if (checked[i])
        continue;

      if (A[i].empty()) {
        continue;
      } else {
        fin = false;
      }

      if (A[A[i].front()].front() == i && !checked[A[i].front()]) {
        checked[i] = true;
        checked[A[i].front()] = true;
        v2.push_back(i);
        v2.push_back(A[i].front());

        A[A[i].front()].pop();
        A[i].pop();
        loop = false;
      }
    }
    if (fin)
      break;
    if (loop) {
      cout << -1 << endl;
      return 0;
    }
    v1.clear();
    v1.swap(v2);
    res++;
  }
  cout << res << endl;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int M = 1e3 + 10;
int a[M][M];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
    a[i][0] = 1;
  }
  int num = n * (n - 1) / 2;
  int flag1 = 0;
  int ans = 0;
  int i = 0;
  while (1) {
    ans++;
    int flag2 = 0;
    int used[M] = {0};
    for (int j = 1; j <= n; j++) {
      long long k = a[j][0];
      if (!used[j] && k != n) {
        long long t1 = a[j][k];
        long long k2 = a[t1][0];
        long long t2 = a[t1][k2];
        if (t2 == j && !used[t1]) {
          a[t1][0]++;
          a[j][0]++;
          used[t1] = 1;
          used[j] = 1;
          flag2 = 1;
          i++;
        }
      }
    }
    if (!flag2) {
      flag1 = 1;
      break;
    }
    if (i == num) {
      break;
    }
  }
  if (!flag1) {
    printf("%d\n", ans);
  } else {
    printf("-1\n");
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int M = 2e3 + 10;
int a[M][M];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
    a[i][0] = 1;
  }
  int num = n * (n - 1) / 2;
  int flag1 = 0;
  int ans = 0;
  int i = 0;
  while (1) {
    ans++;
    int flag2 = 0;
    int used[M] = {0};
    for (int j = 1; j <= n; j++) {
      long long k = a[j][0];
      if (!used[j] && k != n) {
        long long t1 = a[j][k];
        long long k2 = a[t1][0];
        long long t2 = a[t1][k2];
        if (t2 == j && !used[t1]) {
          a[t1][0]++;
          a[j][0]++;
          used[t1] = 1;
          used[j] = 1;
          flag2 = 1;
          i++;
        }
      }
    }
    if (!flag2) {
      flag1 = 1;
      break;
    }
    if (i == num) {
      break;
    }
  }
  if (!flag1) {
    printf("%d\n", ans);
  } else {
    printf("-1\n");
  }
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

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

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];
    }
  }

  int ans = 0;
  vector<int> next_game(N, 0);
  REP(n, N * (N - 1) / 2) {
    vector<bool> in_game(N, false);
    REP(i, N) {
      if (next_game[i] == N - 1 or in_game[i])
        continue;
      int enemy = A[i][next_game[i]];
      if (in_game[enemy])
        continue;
      if (A[enemy][next_game[enemy]] == i) {
        in_game[i] = in_game[enemy] = true;
        ++next_game[i], ++next_game[enemy];
      }
    }
    ++ans;
    if (ans > 1e+7) {
      cout << N * (N - 1) / 2 << endl;
      return 0;
    }
    if (accumulate(next_game.begin(), next_game.end(), 0) == N * (N - 1))
      break;
  }
  bool ok = true;
  REP(i, N) {
    if (next_game[i] != N - 1) {
      ok = false;
      break;
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

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

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];
    }
  }

  int ans = 0;
  vector<int> next_game(N, 0);
  REP(n, N * (N - 1) / 2) {
    vector<bool> in_game(N, false);
    REP(i, N) {
      if (next_game[i] == N - 1 or in_game[i])
        continue;
      int enemy = A[i][next_game[i]];
      if (in_game[enemy])
        continue;
      if (A[enemy][next_game[enemy]] == i) {
        in_game[i] = in_game[enemy] = true;
        ++next_game[i], ++next_game[enemy];
      }
    }
    ++ans;
    if (ans > 1e+5) {
      if (accumulate(next_game.begin(), next_game.end(), 0) > 100 * N)
        cout << N * (N - 1) / 2 << endl;
      else
        cout << -1 << endl;
      return 0;
    }
    if (accumulate(next_game.begin(), next_game.end(), 0) == N * (N - 1))
      break;
  }
  bool ok = true;
  REP(i, N) {
    if (next_game[i] != N - 1) {
      ok = false;
      break;
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}
Problem p02925 (C++)

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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int n, ans[1000];
queue<int> que[1000];

signed main() {
  cin >> n;
  rep(i, n) rep(j, n - 1) {
    int a;
    cin >> a;
    que[i].push(a - 1);
  }
  bool f = true;
  while (f) {
    f = false;
    rep(i, n) {
      if (que[i].empty())
        continue;
      int dif = que[i].front();
      while (i == que[dif].front()) {
        ans[i] = ans[dif] = max(ans[i], ans[dif]) + 1;
        que[i].pop();
        que[dif].pop();
        if (que[i].size())
          dif = que[i].front();
        f = true;
      }
    }
  }
  int trueans = 0;
  rep(i, n) {
    if (que[i].size()) {
      cout << -1 << endl;
      return 0;
    }
    trueans = max(trueans, ans[i]);
  }
  cout << trueans << endl;
}


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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int n, ans[1000];
queue<int> que[1000];

signed main() {
  cin >> n;
  rep(i, n) rep(j, n - 1) {
    int a;
    cin >> a;
    que[i].push(a - 1);
  }
  bool f = true;
  while (f) {
    f = false;
    rep(i, n) {
      if (que[i].empty())
        continue;
      int dif = que[i].front();
      while (i == que[dif].front()) {
        ans[i] = ans[dif] = max(ans[i], ans[dif]) + 1;
        que[i].pop();
        que[dif].pop();
        if (que[i].size())
          dif = que[i].front();
        else
          break;
        f = true;
      }
    }
  }
  int trueans = 0;
  rep(i, n) {
    if (que[i].size()) {
      cout << -1 << endl;
      return 0;
    }
    trueans = max(trueans, ans[i]);
  }
  cout << trueans << endl;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

int main() {
  ll N;

  cin >> N;

  vector<deque<ll>> tbl(N, deque<ll>(N - 1));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> tbl[i][j];
      tbl[i][j]--;
    } // j
  }   // j

  vector<P> que;

  auto check = [&](ll x) {
    if (tbl[x].size() == 0)
      return;
    ll xcompe = tbl[x].front();
    if (x == tbl[xcompe].front()) {
      if (x > xcompe)
        swap(x, xcompe);
      que.push_back(P(x, xcompe));
    }
  };

  for (int i = 0; i < N; i++) {
    check(i);
  }

  ll ans = 0;
  ll games_done = 0;
  while (que.size() > 0) {
    ans++;
    vector<P> prevQ;
    que.erase(unique(que.begin(), que.end()), que.end());
    swap(prevQ, que);
    for (auto &p : prevQ) {
      games_done++;
      tbl[p.first].pop_front();
      tbl[p.second].pop_front();
    } // p
    for (auto &p : prevQ) {
      check(p.first);
      check(p.second);
    } // p
  }   // while

  if (games_done != N * (N - 1) / 2)
    ans = -1;

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

int main() {
  ll N;

  cin >> N;

  vector<deque<ll>> tbl(N, deque<ll>(N - 1));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> tbl[i][j];
      tbl[i][j]--;
    } // j
  }   // j

  vector<P> que;

  auto check = [&](ll x) {
    if (tbl[x].size() == 0)
      return;
    ll xcompe = tbl[x].front();
    if (x == tbl[xcompe].front()) {
      if (x > xcompe)
        swap(x, xcompe);
      que.push_back(P(x, xcompe));
    }
  };

  for (int i = 0; i < N; i++) {
    check(i);
  }

  ll ans = 0;
  ll games_done = 0;
  while (que.size() > 0) {
    ans++;
    vector<P> prevQ;
    sort(que.begin(), que.end());
    que.erase(unique(que.begin(), que.end()), que.end());
    swap(prevQ, que);
    for (auto &p : prevQ) {
      games_done++;
      tbl[p.first].pop_front();
      tbl[p.second].pop_front();
    } // p
    for (auto &p : prevQ) {
      check(p.first);
      check(p.second);
    } // p
  }   // while

  if (games_done != N * (N - 1) / 2)
    ans = -1;

  cout << ans << endl;

  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1024, INF = 0x3f3f3f3f, M = 2e6 + 5;
//
const ll LNF = 0x3f3f3f3f3f3f3f3f;
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
#define repe(i, u) for (int i = head[u]; i; i = nxt[i])
int n, a[N][N], vis[N];
bool flag;
bool ok() {
  for (int i = 1; i <= n; i++)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  for (int z = 0; z <= 0x3f3f3f3f; z++) {
    if (ok()) {
      cout << z << '\n';
      return 0;
    }
    flag = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) {
        a[i][0]++;
        a[x][0]++;
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1023, INF = 0x3f3f3f3f, M = 2e6 + 5;
//
const ll LNF = 0x3f3f3f3f3f3f3f3f;
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
#define repe(i, u) for (int i = head[u]; i; i = nxt[i])
int n, a[N][N], vis[N];
bool flag;
bool ok() {
  for (int i = 1; i <= n; i++)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  for (int z = 0; z <= 0x3f3f3f3f; z++) {
    if (ok()) {
      cout << z << '\n';
      return 0;
    }
    flag = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) {
        a[i][0]++;
        a[x][0]++;
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, INF = 0x3f3f3f3f, M = 2e6 + 5;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
#define repe(i, u) for (int i = head[u]; i; i = nxt[i])
int n, a[1025][1025], vis[1025];
bool flag;
bool ok() {
  for (int i = 1; i <= n; i++)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  for (int z = 0; z <= 0x3f3f3f3f; z++) {
    if (ok()) {
      cout << z << '\n';
      return 0;
    }
    flag = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) {
        a[i][0]++;
        a[x][0]++;
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      return 0;
    }
  }
  return 0;
}


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

int n, a[1025][1025], vis[1025];
bool flag;
bool ok() {
  for (int i = 1; i <= n; i++)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  for (int z = 0; z <= 0x3f3f3f3f; z++) {
    if (ok()) {
      cout << z << '\n';
      return 0;
    }
    flag = 0;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (!vis[i] && !vis[x] && a[x][a[x][0]] == i) {
        a[i][0]++;
        a[x][0]++;
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

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

int n, a[1024][1024], z;
bool vis[1024], flag;
bool ok() {
  for (int i = 1; i <= n; i++)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (ok()) {
      cout << z << '\n';
      return 0;
    }
    flag = 0;
    z++;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) {
        a[i][0]++;
        a[x][0]++;
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      return 0;
    }
  }
  return 0;
}


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

int n, a[1025][1025], z;
bool vis[1025], flag;
bool ok() {
  for (int i = 1; i <= n; i++)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (ok()) {
      cout << z << '\n';
      return 0;
    }
    flag = 0;
    z++;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) {
        a[i][0]++;
        a[x][0]++;
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      cout << -1 << '\n';
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[20000][20000], t = 0;
bool flag[20000], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[11000][11000], t = 0;
bool flag[11000], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[8192][8192], t = 0;
bool flag[8192], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[11000][11000], t = 0;
bool flag[11000], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[2049][2049], t = 0;
bool flag[2049], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[1025][1025], t = 0;
bool flag[1025], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[6025][6025], t = 0;
bool flag[6025], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[1025][1025], t = 0;
bool flag[1025], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[1024][1024], t = 0;
bool flag[1024], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[1025][1025], t = 0;
bool flag[1025], fl = 0;
bool judge() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < n; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (judge()) {
      printf("%d\n", t);
      return 0;
    }
    fl = 0;
    memset(flag, 0, sizeof(flag));
    ++t;
    for (int i = 1; i <= n; ++i) {
      if (flag[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]], y = a[x][a[x][0]];
      if (!flag[x] && !flag[i] && i == y) {
        fl = 1;
        flag[x] = 1;
        flag[i] = 1;
        ++a[i][0];
        ++a[x][0];
      }
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

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

int n, a[1005][1005], z;
bool vis[1005], flag;
bool ok() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (ok()) {
      printf("%d\n", z);
      return 0;
    }
    flag = 0;
    z++;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; ++i) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) {
        ++a[i][0];
        ++a[x][0];
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}


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

int n, a[10010][10010], z;
bool vis[10010], flag;
bool ok() {
  for (int i = 1; i <= n; ++i)
    if (a[i][0] != n)
      return 0;
  return 1;
}

int main() {
  // ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n - 1; ++j)
      cin >> a[i][j];
    a[i][0] = 1;
  }
  while (1) {
    if (ok()) {
      printf("%d\n", z);
      return 0;
    }
    flag = 0;
    z++;
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; ++i) {
      if (vis[i] || a[i][0] == n)
        continue;
      int x = a[i][a[i][0]];
      if (a[x][a[x][0]] == i && !vis[i] && !vis[x]) {
        ++a[i][0];
        ++a[x][0];
        vis[i] = vis[x] = 1;
        flag = 1;
      }
    }
    if (!flag) {
      printf("-1\n");
      return 0;
    }
  }
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = n; i < (int)(m); i++)
#define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i, 0, n) rep(j, 0, n - 1) {
    cin >> a[i][j];
    a[i][j]--;
  }

  int ans = 0;
  vector<int> now_index(n, 0);
  bool finish = false;
  while (true) {
    finish = true;

    vector<int> add(n, 0);
    rep(i, 0, n) {
      if (now_index[i] >= n - 1)
        continue;

      int now = a[i][now_index[i]];

      if (a[now][now_index[now]] == i) {
        add[i]++;
        finish = false;
      }
    }
    rep(i, 0, n) { now_index[i] += add[i]; }

    if (finish)
      break;
    ans++;
  }

  rep(i, 0, n) {
    if (now_index[i] < n - 1)
      ans = -1;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = n; i < (int)(m); i++)
#define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i, 0, n) rep(j, 0, n - 1) {
    cin >> a[i][j];
    a[i][j]--;
  }

  int ans = 0;
  vector<int> now_index(n, 0);
  bool finish = false;
  while (true) {
    finish = true;

    vector<int> add(n, 0);
    rep(i, 0, n) {
      if (now_index[i] >= n - 1)
        continue;

      int now = a[i][now_index[i]];

      if (a[now][now_index[now]] == i) {
        add[i]++;
        finish = false;
      }
    }
    rep(i, 0, n) { now_index[i] += add[i]; }

    if (finish)
      break;
    ans++;
    if (ans >= 100000) {
      cout << n * (n - 1) / 2 << endl;
      return 0;
    }
  }

  rep(i, 0, n) {
    if (now_index[i] < n - 1)
      ans = -1;
  }
  cout << ans << endl;
}