Problem p03166 (C++)

Buggy submission (status: Runtime Error):
// srinivas
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define fr(x, in, n, r) for (ll x = in; x < n; x += r)
#define ifr(x, n) for (ll x = 0; x < n; x++)
#define dfr(x, n) for (ll x = n - 1; x >= 0; x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fstm(m, n, r)                                                          \
  m.reserve(n);                                                                \
  m.max_load_factor(r)
#define os                                                                     \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
typedef long long ll;
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef unordered_map<ll, ll> umap;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 100001
vll v[N];
bool vis[N];
ll dp[N];

void dfs(ll u) {
  vis[u] = 1;
  ifr(i, v[u].size()) {
    if (!vis[v[u][i]])
      dfs(v[u][i]);
    dp[u] = max(dp[u], 1 + dp[v[u][i]]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("/home/srinivas/Desktop/cp/input.txt", "r", stdin);
  // freopen("/home/srinivas/Desktop/output.txt","w",stdout);
#endif
  ll n, m, x, y;
  cin >> n >> m;
  ifr(i, m) {
    cin >> x >> y;
    v[x].pb(y);
  }
  ifr(i, n) {
    if (!vis[i + 1])
      dfs(i + 1);
  }
  ll ans = -1;
  ifr(i, n) ans = max(ans, dp[i + 1]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
// srinivas
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define fr(x, in, n, r) for (ll x = in; x < n; x += r)
#define ifr(x, n) for (ll x = 0; x < n; x++)
#define dfr(x, n) for (ll x = n - 1; x >= 0; x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fstm(m, n, r)                                                          \
  m.reserve(n);                                                                \
  m.max_load_factor(r)
#define os                                                                     \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
typedef long long ll;
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef unordered_map<ll, ll> umap;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 100001
vll v[N];
bool vis[N];
ll dp[N];

void dfs(ll u) {
  vis[u] = 1;
  ifr(i, v[u].size()) {
    if (!vis[v[u][i]])
      dfs(v[u][i]);
    dp[u] = max(dp[u], 1 + dp[v[u][i]]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, x, y;
  cin >> n >> m;
  ifr(i, m) {
    cin >> x >> y;
    v[x].pb(y);
  }
  ifr(i, n) {
    if (!vis[i + 1])
      dfs(i + 1);
  }
  ll ans = -1;
  ifr(i, n) ans = max(ans, dp[i + 1]);
  cout << ans << endl;
  return 0;
}