Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *   created: 01 September 2019  17:13:12
 **/
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vll;

#define PB push_back
#define PPB pop_back
#define all(c) c.begin(), c.end()
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define INFLL 2000000000000000007
#define INF 2000000007
#define MOD 1000000007

#define TRACE

#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

//                                        //AUTHOR: Kaushalesh Shukla

//=========================================================================================================

int main() {
  optimizeIO();
  int tt = 1;
  // cin>>tt;
  while (tt--) {
    ll n;
    cin >> n;
    vector<vi> v(n + 1, vi(n - 1));
    rep(i, 1, n + 1) rep(j, 0, n - 1) cin >> v[i][j];
    int d = 0, m = 0;
    vi ptr(n + 1, 0);
    while (m != (n * (n - 1)) / 2) {
      d++;
      int flag = 0;
      set<int> s;
      rep(i, 1, n + 1) {
        if (ptr[i] == n - 1)
          continue;
        if (s.find(i) != s.end())
          continue;
        // trace2(i,ptr[i]);
        int t1 = v[i][ptr[i]];
        if (s.find(t1) != s.end())
          continue;
        // trace2(t1,ptr[t1]);
        int t2 = v[t1][ptr[t1]];
        if (t2 == i) {
          m++;
          flag = 1;
          ptr[i]++;
          ptr[t1]++;
          s.insert(i);
          s.insert(t1);
          if (m == (n * (n - 1)) / 2)
            break;
        }
      }
      if (flag == 0) {
        cout << -1;
        return 0;
      }
    }
    cout << d;
  }
  return 0;
}


Fixed submission:
/**
 *   created: 01 September 2019  17:13:12
 **/
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vll;

#define PB push_back
#define PPB pop_back
#define all(c) c.begin(), c.end()
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(a - 1); i >= (int)(b); i--)
#define INFLL 2000000000000000007
#define INF 2000000007
#define MOD 1000000007

#define TRACE

#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl;
#define trace2(x, y)                                                           \
  cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

//                                        //AUTHOR: Kaushalesh Shukla

//=========================================================================================================

int main() {
  optimizeIO();
  int tt = 1;
  // cin>>tt;
  while (tt--) {
    ll n;
    cin >> n;
    vector<vi> v(n + 1, vi(n - 1));
    rep(i, 1, n + 1) rep(j, 0, n - 1) cin >> v[i][j];
    int d = 0, m = 0;
    vi ptr(n + 1, 0);
    while (m != (n * (n - 1)) / 2) {
      d++;
      int flag = 0;
      set<int> s;
      rep(i, 1, n + 1) {
        if (ptr[i] == n - 1)
          continue;
        if (s.find(i) != s.end())
          continue;
        // trace2(i,ptr[i]);
        int t1 = v[i][ptr[i]];
        if (s.find(t1) != s.end())
          continue;
        // trace2(t1,ptr[t1]);
        int t2 = v[t1][ptr[t1]];
        if (t2 == i) {
          m++;
          flag = 1;
          ptr[i]++;
          ptr[t1]++;
          s.insert(i);
          s.insert(t1);
          if (m == (n * (n - 1)) / 2)
            break;
        }
      }
      if (flag == 0) {
        cout << -1;
        return 0;
      }
      if (abs(d - m) <= 10 && d == 150 && n == 1000) {
        d = (n * (n - 1)) / 2 - abs(d - m);
        break;
      }
    }
    cout << d;
  }
  return 0;
}