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 std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vpll> vvpll;

typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_set;

#define pb push_back
#define pf push_front
#define FOR(i, x, y, z) for (i = (x); i < (y); i += (z))
#define FOB(i, x, y, z) for (i = (x); i > (y); i -= (z))
#define REF(i, x) FOR(i, 0, x, 1)
#define REB(i, x) FOB(i, x - 1, -1, 1)
#define all(c) c.begin(), c.end()
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define rtr(c, it) for (auto it = (c).rbegin(); it != (c).rend(); it++)
#define sz(c) (c).size()
#define ff first
#define ss second
#define mp(x, y) make_pair(x, y)
#define MOD 1000000007
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define MAX 100000000

pll add_pair(pll a, pll b) { return mp(a.ff + b.ff, a.ss + b.ss); }
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

vvl G((ll)1e5 + 5, vl());
vl indeg((ll)1e5 + 5, 0), outdeg((ll)1e5 + 5, 0), dp((ll)1e5 + 5);
ll ans = 0;

void dfs(ll root) {
  tr(G[root], it) {
    dfs(*it);
    dp[root] = max(dp[root], dp[*it] + 1);
  }
  ans = max(ans, dp[root]);
}

int main() {
  boost;
  ll n, m, i, u, v;
  cin >> n >> m;
  REF(i, m) {
    cin >> u >> v;
    G[u].pb(v);
    indeg[v]++;
    outdeg[u]++;
  }
  REF(i, n) dp[i + 1] = 0;
  REF(i, n) {
    if (indeg[i + 1] == 0) {
      dfs(i + 1);
    }
  }
  cout << ans << '\n';
  return 0;
}

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

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vpll> vvpll;

typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_set;

#define pb push_back
#define pf push_front
#define FOR(i, x, y, z) for (i = (x); i < (y); i += (z))
#define FOB(i, x, y, z) for (i = (x); i > (y); i -= (z))
#define REF(i, x) FOR(i, 0, x, 1)
#define REB(i, x) FOB(i, x - 1, -1, 1)
#define all(c) c.begin(), c.end()
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define rtr(c, it) for (auto it = (c).rbegin(); it != (c).rend(); it++)
#define sz(c) (c).size()
#define ff first
#define ss second
#define mp(x, y) make_pair(x, y)
#define MOD 1000000007
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define MAX 100000000

pll add_pair(pll a, pll b) { return mp(a.ff + b.ff, a.ss + b.ss); }
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

vvl G((ll)1e5 + 5, vl());
vl indeg((ll)1e5 + 5, 0), outdeg((ll)1e5 + 5, 0), dp((ll)1e5 + 5);
ll ans = 0;

void dfs(ll root) {
  if (dp[root] != 0)
    return;
  tr(G[root], it) {
    dfs(*it);
    dp[root] = max(dp[root], dp[*it] + 1);
  }
  ans = max(ans, dp[root]);
}

int main() {
  boost;
  ll n, m, i, u, v;
  cin >> n >> m;
  REF(i, m) {
    cin >> u >> v;
    G[u].pb(v);
    indeg[v]++;
    outdeg[u]++;
  }
  REF(i, n) dp[i + 1] = 0;
  REF(i, n) {
    if (indeg[i + 1] == 0) {
      dfs(i + 1);
    }
  }
  cout << ans << '\n';
  return 0;
}