Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef vector<int> vi;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(v) (int)v.size()
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fill(a, v) memset(a, v, sizeof(a));
#define all(v) v.begin(), v.end()
#define forn(i, a, b) for (int i = a; i < b; i++)

#define trarr(a, n) forn(i, 0, n) cerr << a[i] << " \n"[i == n - 1];
#define trmat(g, n, m)                                                         \
  forn(i, 0, n) forn(j, 0, m) cerr << g[i][j] << " \n"[i == m - 1];
#define tr(...)                                                                \
  cerr << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \
  trace(#__VA_ARGS__, __VA_ARGS__)
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << '(' << p.ff << ", " << p.ss << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &v) {
  int l = v.size();
  for (int i = 0; i < l - 1; i++)
    out << v[i] << ' ';
  if (l > 0)
    out << v[l - 1];
  return out;
}
template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

const ld PI = acos(-1);
const ld EPS = 1e-9;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int P = 1e9 + 7;
const int N = 2e5 + 10;

vector<int> adj[N];
int dpar[N] = {0};
vector<int> topSort;
bool visited[N] = {0};
void dfs(int u) {
  visited[u] = 1;
  for (auto v : adj[u])
    dfs(v);
  topSort.pb(u);
}
int dp(int u) {
  if (dpar[u] != 0)
    return dpar[u];
  int ret = 0;
  for (auto v : adj[u]) {
    ret = max(ret, dp(v));
  }
  return dpar[u] = ret + 1;
}
void solve(int _t) {
  int n, m;
  cin >> n >> m;
  int u, v;
  forn(i, 0, m) {
    cin >> u >> v;
    adj[u].pb(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  for (int i = sz(topSort) - 1; i >= 0; i--) {
    ans = max(ans, dp(topSort[i]));
  }
  cout << ans - 1 << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  forn(test, 1, t + 1) { solve(test); }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef vector<int> vi;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(v) (int)v.size()
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fill(a, v) memset(a, v, sizeof(a));
#define all(v) v.begin(), v.end()
#define forn(i, a, b) for (int i = a; i < b; i++)

#define trarr(a, n) forn(i, 0, n) cerr << a[i] << " \n"[i == n - 1];
#define trmat(g, n, m)                                                         \
  forn(i, 0, n) forn(j, 0, m) cerr << g[i][j] << " \n"[i == m - 1];
#define tr(...)                                                                \
  cerr << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \
  trace(#__VA_ARGS__, __VA_ARGS__)
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << '(' << p.ff << ", " << p.ss << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &v) {
  int l = v.size();
  for (int i = 0; i < l - 1; i++)
    out << v[i] << ' ';
  if (l > 0)
    out << v[l - 1];
  return out;
}
template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

const ld PI = acos(-1);
const ld EPS = 1e-9;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int P = 1e9 + 7;
const int N = 2e5 + 10;

vector<int> adj[N];
int dpar[N] = {0};
vector<int> topSort;
bool visited[N] = {0};
void dfs(int u) {
  visited[u] = 1;
  for (auto v : adj[u])
    if (!visited[v])
      dfs(v);
  topSort.pb(u);
}
int dp(int u) {
  if (dpar[u] != 0)
    return dpar[u];
  int ret = 0;
  for (auto v : adj[u]) {
    ret = max(ret, dp(v));
  }
  return dpar[u] = ret + 1;
}
void solve(int _t) {
  int n, m;
  cin >> n >> m;
  int u, v;
  forn(i, 0, m) {
    cin >> u >> v;
    adj[u].pb(v);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i);
  }
  for (int i = sz(topSort) - 1; i >= 0; i--) {
    ans = max(ans, dp(topSort[i]));
  }
  cout << ans - 1 << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  forn(test, 1, t + 1) { solve(test); }
  return 0;
}