Problem p03166 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize "trapv"
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1000000007;
#define pb push_back
#define fs first
#define sc second
#define forn(i, k, n) for (int i = k; i < n; ++i)
#define nfor(i, n, k) for (int i = n; i >= k; --i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vvpii vector<vector<pait<int, int>>>
#define all(arr) arr.begin(), arr.end()
#define endl '\n'
#define dbgarr(V, n)                                                           \
  forn(i, 0, n) cout << V[i] << " ";                                           \
  cout << endl;
#define dbgarrrev(V, n)                                                        \
  nfor(i, n, 0) cout << V[i] << " ";                                           \
  cout << endl;
#define dbgmaze(V)                                                             \
  for (auto x : V) {                                                           \
    for (auto y : x)                                                           \
      cout << y << " ";                                                        \
    cout << endl;                                                              \
  }
#define dbg2d(V, n, m)                                                         \
  forn(i, 0, n) {                                                              \
    forn(j, 0, m) cout << V[i][j] << " ";                                      \
    cout << endl;                                                              \
  }
#define N 1005
vi G[N];
bool vis[N] = {false};
int dp[N] = {0};
int ans = -1;
void dfs(int src) {
  vis[src] = true;
  for (auto x : G[src]) {
    if (!vis[x])
      dfs(x);
    dp[src] = max(dp[src], 1 + dp[x]);
  }
  ans = max(ans, dp[src]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  forn(i, 0, m) {
    int u, v;
    cin >> u >> v;
    G[u].pb(v);
  }
  forn(i, 1, n + 1) if (!vis[i]) dfs(i);
  cout << ans << endl;
  return 0;
}

Fixed submission:
// #pragma GCC optimize "trapv"
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1000000007;
#define pb push_back
#define fs first
#define sc second
#define forn(i, k, n) for (int i = k; i < n; ++i)
#define nfor(i, n, k) for (int i = n; i >= k; --i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vvpii vector<vector<pait<int, int>>>
#define all(arr) arr.begin(), arr.end()
#define endl '\n'
#define dbgarr(V, n)                                                           \
  forn(i, 0, n) cout << V[i] << " ";                                           \
  cout << endl;
#define dbgarrrev(V, n)                                                        \
  nfor(i, n, 0) cout << V[i] << " ";                                           \
  cout << endl;
#define dbgmaze(V)                                                             \
  for (auto x : V) {                                                           \
    for (auto y : x)                                                           \
      cout << y << " ";                                                        \
    cout << endl;                                                              \
  }
#define dbg2d(V, n, m)                                                         \
  forn(i, 0, n) {                                                              \
    forn(j, 0, m) cout << V[i][j] << " ";                                      \
    cout << endl;                                                              \
  }
#define N 100005
vi G[N];
bool vis[N] = {false};
int dp[N] = {0};
int ans = -1;
void dfs(int src) {
  vis[src] = true;
  for (auto x : G[src]) {
    if (!vis[x])
      dfs(x);
    dp[src] = max(dp[src], 1 + dp[x]);
  }
  ans = max(ans, dp[src]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  forn(i, 0, m) {
    int u, v;
    cin >> u >> v;
    G[u].pb(v);
  }
  forn(i, 1, n + 1) if (!vis[i]) dfs(i);
  cout << ans << endl;
  return 0;
}