Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// typedef tree<int, null_type, less<int>,
// rb_tree_tag,tree_order_statistics_node_update>  ordered_set;
#define ll long long
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout << #x << ": " << x << endl

#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector<ll> v[100005];
ll dp[100005];
ll mark[100005];
ll ans = 0;
ll dfs(ll n) {
  mark[n] = 1;
  ll mx = 0;
  rep(i, v[n].size()) {
    ll x = v[n][i];
    if (mark[x]) {
      mx = max(mx, dp[x]);
    } else {
      mx = max(mx, dfs(x));
    }
  }
  ans = max(ans, mx + 1);
  return dp[n] = mx + 1;
}
#define INF 1000000000000000000
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  long double pi = 3.14159265358979323846;
  ll n, m;
  cin >> n >> m;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].pb(b);
  }
  rep(i, n) {
    rep(j, v[i].size()) {
      if (!mark[v[i][j]]) {
        dfs(v[i][j]);
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;

// typedef tree<int, null_type, less<int>,
// rb_tree_tag,tree_order_statistics_node_update>  ordered_set;
#define ll long long
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout << #x << ": " << x << endl

#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector<ll> v[100005];
ll dp[100005];
ll mark[100005];
ll ans = 0;
ll dfs(ll n) {
  mark[n] = 1;
  ll mx = 0;
  rep(i, v[n].size()) {
    ll x = v[n][i];
    if (mark[x]) {
      mx = max(mx, dp[x]);
    } else {
      mx = max(mx, dfs(x));
    }
  }
  ans = max(ans, mx + 1);
  return dp[n] = mx + 1;
}
#define INF 1000000000000000000
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long double pi = 3.14159265358979323846;
  ll n, m;
  cin >> n >> m;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].pb(b);
  }
  rep(i, n) {
    rep(j, v[i].size()) {
      if (!mark[v[i][j]]) {
        dfs(v[i][j]);
      }
    }
  }
  cout << ans;
  return 0;
}