Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef std::vector<ll> lls;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 998244353L;
template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  for (ll i = S; i <= E; i++)
    chmax(m, v[i]);
  return m;
} // v[S]～v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  for (ll i = S; i <= E; i++)
    chmin(m, v[i]);
  return m;
}
template <class T> inline T MaxE(vector<T> &v, ll N) {
  return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
  return MinE(v, 0, N - 1);
} // 先頭N個中の最小値
template <class T> inline T MaxE(vector<T> &v) {
  T m = v[0];
  for (T e : v)
    chmax(m, e);
  return m;
}
template <class T> inline T MinE(vector<T> &v) {
  T m = v[0];
  for (T e : v)
    chmin(m, e);
  return m;
}

void cinv(lls &llVec, ll n, bool to_0_origin = false) {
  ll val;
  llVec.reserve((size_t)(n));
  for (ll i = 0; i < n; i++) {
    cin >> val;
    if (to_0_origin)
      val--;
    llVec.push_back(val);
  }
}

ll rec(ll v, std::vector<lls> &G, lls &tbl) {
  if (tbl[v] != -INF)
    return tbl[v];

  ll maxval = 0;
  for (auto vv : G[v]) {
    chmax(maxval, rec(vv, G, tbl) + 1);
  }
  return maxval;
}

int main() {
  ll N, M;
  cin >> N >> M;
  std::vector<lls> G(N);

  for (ll i = 0; i < M; i++) {
    ll xx, yy;
    cin >> xx >> yy;
    xx--;
    yy--;
    G[xx].push_back(yy);
  }

  // テーブル初期化
  lls tbl(N, -INF);

  // メモ化再帰
  ll maxval = -INF;
  for (ll v = 0; v < N; v++) {
    chmax(maxval, rec(v, G, tbl));
  }

  cout << maxval << '\n';

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef std::vector<ll> lls;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 998244353L;
template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline T MaxE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  for (ll i = S; i <= E; i++)
    chmax(m, v[i]);
  return m;
} // v[S]～v[E]の最大値
template <class T> inline T MinE(vector<T> &v, ll S, ll E) {
  T m = v[S];
  for (ll i = S; i <= E; i++)
    chmin(m, v[i]);
  return m;
}
template <class T> inline T MaxE(vector<T> &v, ll N) {
  return MaxE(v, 0, N - 1);
} // 先頭N個中の最大値
template <class T> inline T MinE(vector<T> &v, ll N) {
  return MinE(v, 0, N - 1);
} // 先頭N個中の最小値
template <class T> inline T MaxE(vector<T> &v) {
  T m = v[0];
  for (T e : v)
    chmax(m, e);
  return m;
}
template <class T> inline T MinE(vector<T> &v) {
  T m = v[0];
  for (T e : v)
    chmin(m, e);
  return m;
}

void cinv(lls &llVec, ll n, bool to_0_origin = false) {
  ll val;
  llVec.reserve((size_t)(n));
  for (ll i = 0; i < n; i++) {
    cin >> val;
    if (to_0_origin)
      val--;
    llVec.push_back(val);
  }
}

ll rec(ll v, std::vector<lls> &G, lls &tbl) {
  if (tbl[v] != -INF)
    return tbl[v];

  ll maxval = 0;
  for (auto vv : G[v]) {
    chmax(maxval, rec(vv, G, tbl) + 1);
  }
  tbl[v] = maxval;
  return maxval;
}

int main() {
  ll N, M;
  cin >> N >> M;
  std::vector<lls> G(N);

  for (ll i = 0; i < M; i++) {
    ll xx, yy;
    cin >> xx >> yy;
    xx--;
    yy--;
    G[xx].push_back(yy);
  }

  // テーブル初期化
  lls tbl(N, -INF);

  // メモ化再帰
  ll maxval = -INF;
  for (ll v = 0; v < N; v++) {
    chmax(maxval, rec(v, G, tbl));
  }

  cout << maxval << '\n';

  return 0;
}