Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
#define endl "\n"
#define ll long long int
#define vll vector<ll>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
#define tll tuple<ll, ll, ll>
#define mt make_tuple
#define INF LONG_LONG_MAX
#define mod 1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define cases                                                                  \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define array(a, n)                                                            \
  ll a[n];                                                                     \
  rep(i, 0, n) cin >> a[i];
#define vector(a, n)                                                           \
  vll a(n);                                                                    \
  rep(i, 0, n) cin >> a[i];
#define sortall(a) sort(a.begin(), a.end())
#define printall(a, n)                                                         \
  rep(i, 0, n) cout << a[i] << " ";                                            \
  cout << endl;

// --------------- My Template End --------------- //
class Graph {
  map<int, vector<int>> adjList;
  int nodes;
  map<int, int> dp;

public:
  Graph(int node) { nodes = node; }
  void addEdge(int u, int v) { adjList[u].pb(v); }
  void print() {
    for (auto j : adjList) {
      cout << j.ff << " ->";
      for (auto i : j.ss) {
        cout << i << "-";
      }
      cout << endl;
    }
  }
  int dfshelper(int u) {
    if (dp.count(u))
      return dp[u];

    int max1 = 0;
    for (auto j : adjList[u]) {
      // cout<<j<<" "<<curr<<endl;
      max1 = max(max1, dfshelper(j) + 1);
    }
    // cout<<"Returend From"<<u<<" value->"<<curr<<endl;
    return dp[u] = max1;
  }
  int dfs() {
    int max1 = INT_MIN;
    rep(i, 1, nodes + 1) {
      max1 = max(max1, dfshelper(i));
      // cout<<max1<<endl;
    }
    return max1;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  input();
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    g.addEdge(u, v);
  }
  cout << g.dfs() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

void input() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
#define endl "\n"
#define ll long long int
#define vll vector<ll>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
#define tll tuple<ll, ll, ll>
#define mt make_tuple
#define INF LONG_LONG_MAX
#define mod 1000000007
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define cases                                                                  \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define array(a, n)                                                            \
  ll a[n];                                                                     \
  rep(i, 0, n) cin >> a[i];
#define vector(a, n)                                                           \
  vll a(n);                                                                    \
  rep(i, 0, n) cin >> a[i];
#define sortall(a) sort(a.begin(), a.end())
#define printall(a, n)                                                         \
  rep(i, 0, n) cout << a[i] << " ";                                            \
  cout << endl;

// --------------- My Template End --------------- //
class Graph {
  map<int, vector<int>> adjList;
  int nodes;
  map<int, int> dp;

public:
  Graph(int node) { nodes = node; }
  void addEdge(int u, int v) { adjList[u].pb(v); }
  void print() {
    for (auto j : adjList) {
      cout << j.ff << " ->";
      for (auto i : j.ss) {
        cout << i << "-";
      }
      cout << endl;
    }
  }
  int dfshelper(int u) {
    if (dp.count(u))
      return dp[u];

    int max1 = 0;
    for (auto j : adjList[u]) {
      // cout<<j<<" "<<curr<<endl;
      max1 = max(max1, dfshelper(j) + 1);
    }
    // cout<<"Returend From"<<u<<" value->"<<curr<<endl;
    return dp[u] = max1;
  }
  int dfs() {
    int max1 = INT_MIN;
    rep(i, 1, nodes + 1) {
      max1 = max(max1, dfshelper(i));
      // cout<<max1<<endl;
    }
    return max1;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // input();
  int n, m;
  cin >> n >> m;
  Graph g(n);
  rep(i, 0, m) {
    int u, v;
    cin >> u >> v;
    g.addEdge(u, v);
  }
  cout << g.dfs() << endl;
}