Problem p03166 (C++)

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define vvi vector<vector<int>>;
#define mi map<int, int>
#define mii map<pii, int>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sz(x) (int)x.size()
#define all(v) (v).begin(), (v).end()
#define ret(x) return cout << x, 0;
#define rety return cout << "YES", 0;
#define retn return cout << "NO", 0;
#define fl fflush(stdout)
#define hell 1000000007
#define hell2 998244353
#define pi 3.14159265358979323846
vi g[100005], vis(10005), dp(10005);

void dfs(int v, int p) {
  vis[v] = 1;
  for (auto u : g[v]) {
    if (!vis[u])
      dfs(u, v);
    dp[v] = max(dp[v], dp[u] + 1);
  }
}

int solve() {
  int n, m, u, v;
  cin >> n >> m;
  rep(i, 0, m) {
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
  }
  int ans = 0;
  rep(i, 0, n) {
    if (!vis[i])
      dfs(i, i);
    ans = max(ans, dp[i]);
    //  cout<<dp[i]<<' ';
  }
  cout << ans;
  return 0;
}

signed main() {
  IOS;
  int t = 1;

  // cin>>t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define vvi vector<vector<int>>;
#define mi map<int, int>
#define mii map<pii, int>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sz(x) (int)x.size()
#define all(v) (v).begin(), (v).end()
#define ret(x) return cout << x, 0;
#define rety return cout << "YES", 0;
#define retn return cout << "NO", 0;
#define fl fflush(stdout)
#define hell 1000000007
#define hell2 998244353
#define pi 3.14159265358979323846
vi g[100005], vis(100005), dp(100005);

void dfs(int v, int p) {
  vis[v] = 1;
  for (auto u : g[v]) {
    if (!vis[u])
      dfs(u, v);
    dp[v] = max(dp[v], dp[u] + 1);
  }
}

int solve() {
  int n, m, u, v;
  cin >> n >> m;
  rep(i, 0, m) {
    cin >> u >> v;
    u--;
    v--;
    g[u].pb(v);
  }
  int ans = 0;
  rep(i, 0, n) {
    if (!vis[i])
      dfs(i, i);
    ans = max(ans, dp[i]);
    //  cout<<dp[i]<<' ';
  }
  cout << ans;
  return 0;
}

signed main() {
  IOS;
  int t = 1;

  // cin>>t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}