Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ll_s long
#define mod 1000000007
#define forn(i, start, lim) for (ll i = start; i < lim; i++)
#define forn_d(i, start, lim) for (ll i = start; i >= lim; i--)
#define f first
#define s second
#define pb push_back
#define pf push_front
#define mp make_pair
#define debug1(a)                                                              \
  cout << "------\n" << #a << " = " << a << endl << "------" << endl
#define debug2(a, b)                                                           \
  cout << "--------------\n"                                                   \
       << #a << " = " << a << " ; " << #b << " = " << b << endl                \
       << "--------------" << endl
#define debug3(a, b, c)                                                        \
  cout << "----------------------\n"                                           \
       << #a << " = " << a << " ; " << #b << " = " << b << " ; " << #c         \
       << " = " << c << endl                                                   \
       << "----------------------" << endl
#define sz(a) (ll)(a.size())
#define debug4(a) cout << "chu " << a << endl
#define endl "\n"

vector<ll> adj[100005];
bool visited[100005];
ll dp[100005];

void dfs(ll s) {
  for (auto it : adj[s]) {
    if (!visited[it])
      dfs(it);
    dp[s] = max(dp[s], dp[it] + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, m, x, y;
  cin >> n >> m;
  forn(i, 0, m) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  ll ans = 0;
  forn(i, 1, n + 1) ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ll_s long
#define mod 1000000007
#define forn(i, start, lim) for (ll i = start; i < lim; i++)
#define forn_d(i, start, lim) for (ll i = start; i >= lim; i--)
#define f first
#define s second
#define pb push_back
#define pf push_front
#define mp make_pair
#define debug1(a)                                                              \
  cout << "------\n" << #a << " = " << a << endl << "------" << endl
#define debug2(a, b)                                                           \
  cout << "--------------\n"                                                   \
       << #a << " = " << a << " ; " << #b << " = " << b << endl                \
       << "--------------" << endl
#define debug3(a, b, c)                                                        \
  cout << "----------------------\n"                                           \
       << #a << " = " << a << " ; " << #b << " = " << b << " ; " << #c         \
       << " = " << c << endl                                                   \
       << "----------------------" << endl
#define sz(a) (ll)(a.size())
#define debug4(a) cout << "chu " << a << endl
#define endl "\n"

vector<ll> adj[100005];
bool visited[100005];
ll dp[100005];

void dfs(ll s) {
  visited[s] = true;
  for (auto it : adj[s]) {
    if (!visited[it])
      dfs(it);
    dp[s] = max(dp[s], dp[it] + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, m, x, y;
  cin >> n >> m;
  forn(i, 0, m) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  for (ll i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  ll ans = 0;
  forn(i, 1, n + 1) ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}