Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
////////////////////////////////////////////////*kedar1514*////////////////////////////////////////////////(Template
///Start)*****
#define endl "\n"
#define ll long long
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, n) for (i = 1; i <= n; i++)
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sortAll(x) sort(all(x))
#define clearAll(arr) memset(arr, 0, sizeof(arr))
#define deb(sum) cout << "deb = " << sum << endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define kedar                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

ll powerMod(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

ll power(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * x);
    x = (x * x);
    n >>= 1;
  }
  return res;
}

int gcd(int A, int B) {
  if (B == 0)
    return A;
  else
    return gcd(B, A % B);
}
////////////////////////////////////////////////*kedar1514*////////////////////////////////////////////////(Template
///ends)*****
//  <<,
//  , cout<<<<endl;
int dp[100005];
vi gp[100005];

int dfs(int a) {
  int ret = 0;
  if (dp[a] != -1)
    return dp[a];
  for (int i = 0; i < gp[a].size(); i++) {
    ret = max(ret, dfs(gp[a][i]) + 1);
  }
  return dp[a] = ret;
}

void solve() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    --x;
    --y;
    gp[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    ret = max(ret, dfs(i));
  }
  cout << ret;
}

int main() {
  kedar;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // <<
  //  <<,
  //  ,

  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
} // <<
//  <<,
//  ,


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
////////////////////////////////////////////////*kedar1514*////////////////////////////////////////////////(Template
///Start)*****
#define endl "\n"
#define ll long long
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, n) for (i = 1; i <= n; i++)
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sortAll(x) sort(all(x))
#define clearAll(arr) memset(arr, 0, sizeof(arr))
#define deb(sum) cout << "deb = " << sum << endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define kedar                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

ll powerMod(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}

ll power(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * x);
    x = (x * x);
    n >>= 1;
  }
  return res;
}

int gcd(int A, int B) {
  if (B == 0)
    return A;
  else
    return gcd(B, A % B);
}
////////////////////////////////////////////////*kedar1514*////////////////////////////////////////////////(Template
///ends)*****
//  <<,
//  , cout<<<<endl;
int dp[100005];
vi gp[100005];

int dfs(int a) {
  int ret = 0;
  if (dp[a] != -1)
    return dp[a];
  for (int i = 0; i < gp[a].size(); i++) {
    ret = max(ret, dfs(gp[a][i]) + 1);
  }
  return dp[a] = ret;
}

void solve() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    --x;
    --y;
    gp[x].push_back(y);
  }
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    ret = max(ret, dfs(i));
  }
  cout << ret;
}

int main() {
  kedar;
  // <<
  //  <<,
  //  ,

  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
} // <<
//  <<,
//  ,