Problem p03166 (C++)

Buggy submission (status: Runtime Error):
/*--ILRS-- sr4saurabh  */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define popb pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 10000000 + 6;
#define M_PI 3.14159265358979323846
vii adj[100001];
int visited[100001] = {0};
int d[100001] = {0};
void dfs(int node) {
  // cout<<node<<endl;
  visited[node] = 1;
  for (int v : adj[node]) {
    if (!visited[v]) {
      dfs(v);
    }
    d[node] = max(d[node], d[v] + 1);
  }
  d[node] = max(1, d[node]);
}
void bfs(int node) {
  queue<int> q;
  q.push(node);
  visited[node] = 1;
  while (!q.empty()) {
    int v = q.front();
    // cout<<v<<endl;
    q.pop();

    for (int g : adj[v])
      if (!visited[g]) {
        visited[g] = 1;
        q.push(g);
      }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif
  // for(int i=0;i<=100000;i++)
  // visited[i]=0;

  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    // adj[v].pb(u);
  }
  for (int j = 1; j <= n; j++) {
    if (!visited[j])
      dfs(j);
  }
  int ans = 0;
  for (int j = 1; j <= n; j++) {
    ans = max(ans, d[j]);
  }
  cout << ans - 1;

  return 0;
}

Fixed submission:
/*--ILRS-- sr4saurabh  */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define popb pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 10000000 + 6;
#define M_PI 3.14159265358979323846
vii adj[100001];
int visited[100001] = {0};
int d[100001] = {0};
void dfs(int node) {
  // cout<<node<<endl;
  visited[node] = 1;
  for (int v : adj[node]) {
    if (!visited[v]) {
      dfs(v);
    }
    d[node] = max(d[node], d[v] + 1);
  }
  d[node] = max(1, d[node]);
}
void bfs(int node) {
  queue<int> q;
  q.push(node);
  visited[node] = 1;
  while (!q.empty()) {
    int v = q.front();
    // cout<<v<<endl;
    q.pop();

    for (int g : adj[v])
      if (!visited[g]) {
        visited[g] = 1;
        q.push(g);
      }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  //  freopen ("INPUT.txt" , "r" , stdin);
  // freopen ("OUTPUT.txt", "w" , stdout);
  // #endif
  // for(int i=0;i<=100000;i++)
  // visited[i]=0;

  int n, m;
  cin >> n >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    // adj[v].pb(u);
  }
  for (int j = 1; j <= n; j++) {
    if (!visited[j])
      dfs(j);
  }
  int ans = 0;
  for (int j = 1; j <= n; j++) {
    ans = max(ans, d[j]);
  }
  cout << ans - 1;

  return 0;
}