Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
// Author -> Preeyadarshee Dev
//
// train hard win easy :D
// nik is love
// nik is motivation
//
// do modulo operations very carefully
// try to avoid binary search, use linear search preferrably to make
// calculations easier always make your custom binary search and watch out for
// overflows read the problems carefully never rely on floating point
// calculation
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define ss second
#define ff first
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define f(i, n) fr(i, 0, n)
#define rf(i, b, a) for (ll i = b - 1; i >= a; i--)
#define r(i, n) rf(i, n, 0)
#define inf 1000000007
#define eps 0.000000000000001
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof(c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define pi pair<int, int>
#define pll pair<ll, ll>
#define endl '\n'
#define PI                                                                     \
  3.141592653589793238462643383279502884197169399375105820974944592307816406286
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef vector<int> vi;
typedef vector<vi> vvi;
int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
vector<int> adj[200010], vis(200010, 0), dis(200010, inf), fin(200010, 0),
    col(200010, 0), par(200010, 0);
int myrandom(int i) { return std::rand() % i; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // ifstream fin;
  // ofstream fout;
  // fin.open("input.txt");
  // fout.open("output.txt");
  int n;
  cin >> n;
  vector<vector<int>> v(n + 1, vector<int>(n, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      cin >> v[i][j];
    }
  }
  vector<int> ptr(n + 1, 1);
  int cnt = 0;
  for (int j = 1; j <= n * n; j++) {
    int flag = 0;
    vector<int> vis(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      if (vis[i] == 0 && ptr[i] < n) {
        int num = v[i][ptr[i]];
        if (ptr[num] < n && i == v[num][ptr[num]] && vis[num] == 0) {
          if (flag == 0) {
            flag = 1;
            vis[num] = 1;
            vis[i] = 1;
            ptr[i]++;
            ptr[num]++;
          } else {
            vis[num] = 1;
            vis[i] = 1;
            ptr[i]++;
            ptr[num]++;
          }
        }
      }
    }
    if (flag == 0) {
      break;
    }
    cnt = j;
  }
  bool ok = true;
  for (int i = 1; i <= n; i++) {
    if (ptr[i] != n)
      ok = false;
  }
  if (!ok)
    cout << -1 << endl;
  else
    cout << cnt << endl;
}


Fixed submission:
// Author -> Preeyadarshee Dev
//
// train hard win easy :D
// nik is love
// nik is motivation
//
// do modulo operations very carefully
// try to avoid binary search, use linear search preferrably to make
// calculations easier always make your custom binary search and watch out for
// overflows read the problems carefully never rely on floating point
// calculation
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define ll long long
#define ss second
#define ff first
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define f(i, n) fr(i, 0, n)
#define rf(i, b, a) for (ll i = b - 1; i >= a; i--)
#define r(i, n) rf(i, n, 0)
#define inf 1000000007
#define eps 0.000000000000001
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof(c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define pi pair<int, int>
#define pll pair<ll, ll>
#define endl '\n'
#define PI                                                                     \
  3.141592653589793238462643383279502884197169399375105820974944592307816406286
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef vector<int> vi;
typedef vector<vi> vvi;
int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
vector<int> adj[200010], vis(200010, 0), dis(200010, inf), fin(200010, 0),
    col(200010, 0), par(200010, 0);
int myrandom(int i) { return std::rand() % i; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // ifstream fin;
  // ofstream fout;
  // fin.open("input.txt");
  // fout.open("output.txt");
  int n;
  cin >> n;
  vector<vector<int>> v(n + 1, vector<int>(n, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
      cin >> v[i][j];
    }
  }
  vector<int> ptr(n + 1, 1);
  int cnt = 0;
  int mx = (n * (n - 1)) / 2;
  for (int j = 1; j <= mx; j++) {
    int flag = 0;
    vector<int> vis(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      if (vis[i] == 0 && ptr[i] < n) {
        int num = v[i][ptr[i]];
        if (ptr[num] < n && i == v[num][ptr[num]] && vis[num] == 0) {
          if (flag == 0) {
            flag = 1;
            vis[num] = 1;
            vis[i] = 1;
            ptr[i]++;
            ptr[num]++;
          } else {
            vis[num] = 1;
            vis[i] = 1;
            ptr[i]++;
            ptr[num]++;
          }
        }
      }
    }
    if (flag == 0) {
      break;
    }
    cnt = j;
  }
  bool ok = true;
  for (int i = 1; i <= n; i++) {
    if (ptr[i] != n)
      ok = false;
  }
  if (!ok)
    cout << -1 << endl;
  else
    cout << cnt << endl;
}