Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define int long long int
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define sz(s) (int)s.size()
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define tiii tuple<int, int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(v) v.rbegin(), v.rend()
#define endl '\n'
#define matrix vector<vector<int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define NAYAN                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define N 1006
#define mod 1000000007
#define mod2 998244353
#define inf 1e18

vector<int> g[N];
int ind[N];
int dist[N];
int vis[N];

void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    ind[v]--;
    dist[v] = max(dist[v], dist[u] + 1);
    if (ind[v] == 0)
      dfs(v);
  }
}

int32_t main() {
  boost
      // #ifndef ONLINE_JUDGE
      // NAYAN
      // #endif

      int n,
      m;
  cin >> n >> m;
  int i;
  int u, v;
  for (i = 1; i <= m; i++) {
    cin >> u >> v;
    g[u].pb(v);
    ind[v]++;
  }

  for (i = 1; i <= n; i++) {
    if (ind[i] == 0 && vis[i] == 0) {
      dfs(i);
    }
  }

  int ans = 0;

  for (i = 1; i <= n; i++) {
    ans = max(ans, dist[i]);
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define int long long int
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define sz(s) (int)s.size()
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define tiii tuple<int, int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(v) v.rbegin(), v.rend()
#define endl '\n'
#define matrix vector<vector<int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define NAYAN                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define N 100006
#define mod 1000000007
#define mod2 998244353
#define inf 1e18

vector<int> g[N];
int ind[N];
int dist[N];
int vis[N];

void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    ind[v]--;
    dist[v] = max(dist[v], dist[u] + 1);
    if (ind[v] == 0)
      dfs(v);
  }
}

int32_t main() {
  boost
      // #ifndef ONLINE_JUDGE
      // NAYAN
      // #endif

      int n,
      m;
  cin >> n >> m;
  int i;
  int u, v;
  for (i = 1; i <= m; i++) {
    cin >> u >> v;
    g[u].pb(v);
    ind[v]++;
  }

  for (i = 1; i <= n; i++) {
    if (ind[i] == 0 && vis[i] == 0) {
      dfs(i);
    }
  }

  int ans = 0;

  for (i = 1; i <= n; i++) {
    ans = max(ans, dist[i]);
  }

  cout << ans;
}