Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ii int
#define jmp cout << "\n"
#define vl vector<ll>
#define pb push_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define printv(v)                                                              \
  for (auto x : v)                                                             \
    cout << x << " ";                                                          \
  jmp;
#define endll "\n"
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define UNI(a) a.erase(unique(a.begin(), a.end()), a.end())
#define si(x) x.size()
#define ff first
#define ss second
#define iin insert
using namespace std;
ll ctr, mx;
ll dp[100005];
ll dfs(vector<vector<ll>> &g, ll s) {
  // cout << ctr << " ";
  if (dp[s] != -1)
    return dp[s];
  bool k = 0;
  ll ms = 0;
  for (auto x : g[s]) {
    k = 0;
    ms = max(ms, dfs(g, x));
  }
  return dp[s] = (k) ? 0 : (1 + ms);
}
void solve() {
  ll n, m, l, r;
  cin >> n >> m;
  vector<vector<ll>> adj(n + 1);
  for (ll i = 0; i < m; i++) {
    cin >> l >> r;
    adj[l].pb(r);
  }
  ll res = 0;
  memset(dp, -1, sizeof(dp));
  for (ll i = 1; i <= n; i++) {
    res = max(res, dfs(adj, i));
  }

  cout << res - 1 << endll;
}
int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // ll t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ii int
#define jmp cout << "\n"
#define vl vector<ll>
#define pb push_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define printv(v)                                                              \
  for (auto x : v)                                                             \
    cout << x << " ";                                                          \
  jmp;
#define endll "\n"
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define UNI(a) a.erase(unique(a.begin(), a.end()), a.end())
#define si(x) x.size()
#define ff first
#define ss second
#define iin insert
using namespace std;
ll ctr, mx;
ll dp[100005];
ll dfs(vector<vector<ll>> &g, ll s) {
  // cout << ctr << " ";
  if (dp[s] != -1)
    return dp[s];
  bool k = 0;
  ll ms = 0;
  for (auto x : g[s]) {
    k = 0;
    ms = max(ms, dfs(g, x));
  }
  return dp[s] = (k) ? 0 : (1 + ms);
}
void solve() {
  ll n, m, l, r;
  cin >> n >> m;
  vector<vector<ll>> adj(n + 1);
  for (ll i = 0; i < m; i++) {
    cin >> l >> r;
    adj[l].pb(r);
  }
  ll res = 0;
  memset(dp, -1, sizeof(dp));
  for (ll i = 1; i <= n; i++) {
    res = max(res, dfs(adj, i));
  }

  cout << res - 1 << endll;
}
int main() {
  fast;
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  // ll t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}