Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll, ll>
#define md (ll)(1e9 + 7)
#define all(X) X.begin(), X.end()

// for pair comparison function(ascending order) use return (i1.ff < i2.ff);

/* string operations :
   str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
   str.substr (x) : returns a substring str[x],... end of string
   str.find(qtr) : returns the first occurenece of qtr in str */

const int M = 1e6 + 6;

ll pwr(ll a, ll n, ll m) {
  ll p = 1;
  while (n > 0) {
    if (n % 2 == 1)
      p = (p * a) % m;
    a = (a * a) % m;
    n = n / 2;
  }
  return p;
}

ll vis[M], dp[M];
vector<ll> tope, adj[M], rdj[M];

void dfs(ll u) {
  vis[u] = 1;
  for (auto v : adj[u])
    dfs(v);
  tope.pb(u);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m, i, u, v, mx = 0;

  cin >> n >> m;

  for (i = 1; i <= m; ++i) {
    cin >> u >> v;
    adj[u].pb(v);
    rdj[v].pb(u);
  }

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

  reverse(all(tope));
  for (auto u : tope) {
    for (auto v : rdj[u]) {
      dp[u] = max(dp[u], dp[v] + 1);
      mx = max(mx, dp[u]);
    }
  }

  cout << mx << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll, ll>
#define md (ll)(1e9 + 7)
#define all(X) X.begin(), X.end()

// for pair comparison function(ascending order) use return (i1.ff < i2.ff);

/* string operations :
   str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
   str.substr (x) : returns a substring str[x],... end of string
   str.find(qtr) : returns the first occurenece of qtr in str */

const int M = 1e6 + 6;

ll pwr(ll a, ll n, ll m) {
  ll p = 1;
  while (n > 0) {
    if (n % 2 == 1)
      p = (p * a) % m;
    a = (a * a) % m;
    n = n / 2;
  }
  return p;
}

ll vis[M], dp[M];
vector<ll> tope, adj[M], rdj[M];

void dfs(ll u) {
  vis[u] = 1;
  for (auto v : adj[u]) {
    if (vis[v] == 0)
      dfs(v);
  }
  tope.pb(u);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, m, i, u, v, mx = 0;

  cin >> n >> m;

  for (i = 1; i <= m; ++i) {
    cin >> u >> v;
    adj[u].pb(v);
    rdj[v].pb(u);
  }

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

  reverse(all(tope));
  for (auto u : tope) {
    for (auto v : rdj[u]) {
      dp[u] = max(dp[u], dp[v] + 1);
      mx = max(mx, dp[u]);
    }
  }

  cout << mx << endl;
  return 0;
}