Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define ar array
#define bigint int64_t
#define vll vector<ll>
#define pll ar<ll, 2>
#define vpll vector<pll>
#define pb push_back
#define MOD 1000000007
#define sz(x) (ll)(x).size()
#define ff first
#define ss second
#define en cout << "\n"
#define LCM(a, b) (a * (b / __gcd(a, b)))
#define all(v) v.begin(), v.end()
#define maxqueue priority_queue<int>
#define minqueue priority_queue<int, vector<int>, greater<int>>
#define lb(v, val) (lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (upper_bound(v.begin(), v.end(), val) - v.begin())
#define INF (ll)(1e18)
#define ini(a, b) memset(a, b, sizeof(a))
#define vvll vector<vector<ll>>
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define prec(n) fixed << setprecision(n)
#define F_OR(i, a, b, s)                                                       \
  for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define forn(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define deb1(x) cerr << #x << ": " << x << endl
#define deb2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define deb3(x, y, z)                                                          \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define deb4(a, b, c, d)                                                       \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define deb5(a, b, c, d, e)                                                    \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define deb6(a, b, c, d, e, f)                                                 \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define GET5(a, b, c, d, e, ...) e
#define D_EBC(...) GET5(__VA_ARGS__, deb4, deb3, deb2, deb1)
#define deb(...) D_EBC(__VA_ARGS__)(__VA_ARGS__)
#define each(x, a) for (auto &x : a)

using namespace std;

template <typename T> void read(T &x) { cin >> x; }
template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; }

template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) {
  cin >> x >> y >> z;
}

template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
  cin >> x >> y >> z >> w;
}

template <typename T> void read(vector<T> &oneD, ll n) {
  for (ll i = 0; i < n; i++) {
    read(oneD[i]);
  }
}

template <typename T> void read(T oneD[], ll n) {
  for (ll i = 0; i < n; i++) {
    read(oneD[i]);
  }
}

const ll N = 1e5;
ll dp[N + 1], n, m;
vll g[N + 1];

void dfs(ll i) {
  each(u, g[i]) {
    dfs(u);
    dp[i] = max(dp[i], dp[u] + 1);
  }
}

void solve() {
  read(n, m);
  forn(i, m) {
    ll a, b;
    read(a, b);
    --a, --b;
    g[a].pb(b);
  }
  ll ans = 0;
  forn(i, n) {
    if (!dp[i])
      dfs(i);
  }
  cout << *(max_element(dp, dp + N));
}

int main() {
  fast;
  int t = 1;
  // read(t);
  forn(t) {
    // cout << "Case #" << i+1 << ": ";
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ar array
#define bigint int64_t
#define vll vector<ll>
#define pll ar<ll, 2>
#define vpll vector<pll>
#define pb push_back
#define MOD 1000000007
#define sz(x) (ll)(x).size()
#define ff first
#define ss second
#define en cout << "\n"
#define LCM(a, b) (a * (b / __gcd(a, b)))
#define all(v) v.begin(), v.end()
#define maxqueue priority_queue<int>
#define minqueue priority_queue<int, vector<int>, greater<int>>
#define lb(v, val) (lower_bound(v.begin(), v.end(), val) - v.begin())
#define ub(v, val) (upper_bound(v.begin(), v.end(), val) - v.begin())
#define INF (ll)(1e18)
#define ini(a, b) memset(a, b, sizeof(a))
#define vvll vector<vector<ll>>
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define prec(n) fixed << setprecision(n)
#define F_OR(i, a, b, s)                                                       \
  for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define forn(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define deb1(x) cerr << #x << ": " << x << endl
#define deb2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define deb3(x, y, z)                                                          \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define deb4(a, b, c, d)                                                       \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define deb5(a, b, c, d, e)                                                    \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define deb6(a, b, c, d, e, f)                                                 \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define GET5(a, b, c, d, e, ...) e
#define D_EBC(...) GET5(__VA_ARGS__, deb4, deb3, deb2, deb1)
#define deb(...) D_EBC(__VA_ARGS__)(__VA_ARGS__)
#define each(x, a) for (auto &x : a)

using namespace std;

template <typename T> void read(T &x) { cin >> x; }
template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; }

template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) {
  cin >> x >> y >> z;
}

template <typename T, typename T0, typename T1, typename T2>
void read(T &x, T0 &y, T1 &z, T2 &w) {
  cin >> x >> y >> z >> w;
}

template <typename T> void read(vector<T> &oneD, ll n) {
  for (ll i = 0; i < n; i++) {
    read(oneD[i]);
  }
}

template <typename T> void read(T oneD[], ll n) {
  for (ll i = 0; i < n; i++) {
    read(oneD[i]);
  }
}

const ll N = 1e5;
ll dp[N + 1], n, m;
vll g[N + 1];

void dfs(ll i) {
  each(u, g[i]) {
    if (dp[u]) {
      dp[i] = max(dp[i], dp[u] + 1);
      continue;
    }
    dfs(u);
    dp[i] = max(dp[i], dp[u] + 1);
  }
}

void solve() {
  read(n, m);
  forn(i, m) {
    ll a, b;
    read(a, b);
    --a, --b;
    g[a].pb(b);
  }
  ll ans = 0;
  forn(i, n) {
    if (!dp[i])
      dfs(i);
  }
  cout << *(max_element(dp, dp + N));
}

int main() {
  fast;
  int t = 1;
  // read(t);
  forn(t) {
    // cout << "Case #" << i+1 << ": ";
    solve();
  }
}