Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// namespace mp = boost::multiprecision;
using namespace std;
using ull = unsigned long long;
using ld = long double;
// using ll = boost::multiprecision::cpp_int;
using ll = long long;
#define FOR(i, N) for (ll i = 0; i < N; ++i)

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
// using int = ll;
ll gcd(ll m, ll n) {
  if (n == 0)
    return abs(m);
  return (gcd(n, m % n));
}
void putYN(bool b) {
  if (b) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
void swap(ll &a, ll &b) {
  ll tmp = a;
  a = b;
  b = tmp;
}
ll combi(ll n, ll k) {
  ll r = 1, rr = 1;
  for (ll i = 0; i < k; ++i) {
    r *= (n - i);
    rr *= (i + 1);
    // r %= 1000000007;
    // rr %= 1000000007;
  }
  r /= rr;
  // r %= 1000000007;
  return r;
}
int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> A(N, vector<ll>(N - 1));
  vector<vector<ll>> B(N, vector<ll>(N));
  FOR(i, N) {
    FOR(j, N - 1) {
      cin >> A[i][j];
      A[i][j]--;
      B[i][A[i][j]] = j;
      // iが何番目に入っているかを調べる
    }
  }
  vector<vector<ll>> D(N, vector<ll>(N - 1, -1));
  // 最短日数

  function<ll(ll, ll, ll)> f = [&](ll i, ll n,
                                   ll ii) -> ll { // iがi番目の選手 nが見る番号
    if (ii > N * (N + 1))
      return -2;
    if (n == -1)
      return 0;
    if (D[i][n] != -1)
      return D[i][n];
    ll a = A[i][n]; // もう一つの選手
    ll an = B[a][i];
    ll day1 = f(i, n - 1, ii + 1) + 1;
    ll day2 = f(a, an - 1, ii + 1) + 1;
    if (day1 == -1 || day2 == -1)
      return -2;
    if (day1 < day2)
      day1 = day2;
    D[i][n] = day1;
    D[a][an] = day1;
    return day1;
  };
  ll maxd = 0;
  FOR(i, N) {
    ll day = f(i, N - 2, 0);
    if (day > maxd) {
      maxd = day;
    }
    if (day == -2) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << maxd << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <sstream>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// namespace mp = boost::multiprecision;
using namespace std;
using ull = unsigned long long;
using ld = long double;
// using ll = boost::multiprecision::cpp_int;
using ll = long long;
#define FOR(i, N) for (ll i = 0; i < N; ++i)

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
// using int = ll;
ll gcd(ll m, ll n) {
  if (n == 0)
    return abs(m);
  return (gcd(n, m % n));
}
void putYN(bool b) {
  if (b) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
void swap(ll &a, ll &b) {
  ll tmp = a;
  a = b;
  b = tmp;
}
ll combi(ll n, ll k) {
  ll r = 1, rr = 1;
  for (ll i = 0; i < k; ++i) {
    r *= (n - i);
    rr *= (i + 1);
    // r %= 1000000007;
    // rr %= 1000000007;
  }
  r /= rr;
  // r %= 1000000007;
  return r;
}
int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> A(N, vector<ll>(N - 1));
  vector<vector<ll>> B(N, vector<ll>(N));
  FOR(i, N) {
    FOR(j, N - 1) {
      cin >> A[i][j];
      A[i][j]--;
      B[i][A[i][j]] = j;
      // iが何番目に入っているかを調べる
    }
  }
  vector<vector<ll>> D(N, vector<ll>(N - 1, -1));
  // 最短日数

  function<ll(ll, ll, ll)> f = [&](ll i, ll n,
                                   ll ii) -> ll { // iがi番目の選手 nが見る番号
    if (ii > N * (N + 1))
      return -2;
    if (n == -1)
      return 0;
    if (D[i][n] != -1)
      return D[i][n];
    ll a = A[i][n]; // もう一つの選手
    ll an = B[a][i];
    ll day1 = f(i, n - 1, ii + 1) + 1;
    ll day2 = f(a, an - 1, ii + 1) + 1;
    if (day1 == -1 || day2 == -1) {
      day1 = -2;
      day2 = -2;
    }
    if (day1 < day2)
      day1 = day2;
    D[i][n] = day1;
    D[a][an] = day1;
    return day1;
  };
  ll maxd = 0;
  FOR(i, N) {
    ll day = f(i, N - 2, 0);
    if (day > maxd) {
      maxd = day;
    }
    if (day == -2) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << maxd << endl;
  return 0;
}