Problem p03166 (C++)

Buggy submission (status: Runtime Error):
/*-------------------------------"ANJAN AGARWALA"-----------------------------

         _/_/_/_/    _/_/    _/   _/_/_/_/   _/_/_/_/    _/_/    _/
        _/    _/    _/ _/   _/         _/   _/    _/    _/ _/   _/
       _/_/_/_/    _/   _/ _/         _/   _/_/_/_/    _/  _/  _/
      _/    _/    _/    _/_/   _/    _/   _/    _/    _/    _/_/
     _/    _/    _/      _/   _/_/_/_/   _/    _/    _/      _/

-------------------------------------@anjn98---------------------------------*/

// #define opti
#ifdef opti
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

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

typedef long long int ll;
typedef long long int LL;
typedef unsigned long long int ull;
ll power(ll x, ll y, ll p) {
  ll r = 1;
  x = x % p;
  while (y) {
    if (y & 1)
      r = r * x % p;
    y = y >> 1;
    x = x * x % p;
  }
  return r;
}
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;*/

#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FILEIO                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define all(v) v.begin(), v.end()
#define PR(a) cout << "(" << a << ")";
#define ED cout << '\n';
// #define endl            '\n'
#define loop(j, a, n) for (int j = a; j <= n; j++)
#define loop1(j, a, n) for (int j = a; j >= n; j--)
#define fr1(j, n) for (int j = 0; j < n; j++)
#define fr(n) for (int i = 0; i < n; i++)
#define trav(x, s) for (auto x : s)
#define SC(n) scanf("%d", &n)
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define F first
#define S second
#define binf 2000000000000000001
#define mod 1000000007

const int N = 1e5 + 99;
int indeg[N], len[N], n, m;
bool vis[N];
std::vector<int> g[N];

void dfs(int v) {
  vis[v] = 1;
  if (g[v].size() == 0) {
    len[v] = 0;
    return;
  }
  trav(x, g[v]) {
    if (!vis[x])
      dfs(x);
    len[v] = max(len[v], 1 + len[x]);
  }
}

int main() {

  IO
#ifndef ONLINE_JUDGE
          FILEIO
#endif
              cin >>
      n >> m;
  fr(m) {
    int x, y;
    cin >> x >> y;
    g[x].PB(y);
    indeg[y]++;
  }
  for (int i = 1; i <= n; i++)
    if (!indeg[i]) {
      dfs(i);
    }

  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, (ll)len[i]);
  cout << ans << endl;
}

Fixed submission:
/*-------------------------------"ANJAN AGARWALA"-----------------------------

         _/_/_/_/    _/_/    _/   _/_/_/_/   _/_/_/_/    _/_/    _/
        _/    _/    _/ _/   _/         _/   _/    _/    _/ _/   _/
       _/_/_/_/    _/   _/ _/         _/   _/_/_/_/    _/  _/  _/
      _/    _/    _/    _/_/   _/    _/   _/    _/    _/    _/_/
     _/    _/    _/      _/   _/_/_/_/   _/    _/    _/      _/

-------------------------------------@anjn98---------------------------------*/

// #define opti
#ifdef opti
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

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

typedef long long int ll;
typedef long long int LL;
typedef unsigned long long int ull;
ll power(ll x, ll y, ll p) {
  ll r = 1;
  x = x % p;
  while (y) {
    if (y & 1)
      r = r * x % p;
    y = y >> 1;
    x = x * x % p;
  }
  return r;
}
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;*/

#define IO                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FILEIO                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define all(v) v.begin(), v.end()
#define PR(a) cout << "(" << a << ")";
#define ED cout << '\n';
// #define endl            '\n'
#define loop(j, a, n) for (int j = a; j <= n; j++)
#define loop1(j, a, n) for (int j = a; j >= n; j--)
#define fr1(j, n) for (int j = 0; j < n; j++)
#define fr(n) for (int i = 0; i < n; i++)
#define trav(x, s) for (auto x : s)
#define SC(n) scanf("%d", &n)
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define F first
#define S second
#define binf 2000000000000000001
#define mod 1000000007

const int N = 1e5 + 99;
int indeg[N], len[N], n, m;
bool vis[N];
std::vector<int> g[N];

void dfs(int v) {
  vis[v] = 1;
  if (g[v].size() == 0) {
    len[v] = 0;
    return;
  }
  trav(x, g[v]) {
    if (!vis[x])
      dfs(x);
    len[v] = max(len[v], 1 + len[x]);
  }
}

int main() {

  IO
          /* #ifndef ONLINE_JUDGE
           FILEIO
           #endif*/
          cin >>
      n >> m;
  fr(m) {
    int x, y;
    cin >> x >> y;
    g[x].PB(y);
    indeg[y]++;
  }
  for (int i = 1; i <= n; i++)
    if (!indeg[i]) {
      dfs(i);
    }

  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, (ll)len[i]);
  cout << ans << endl;
}