Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  loop(test, 1, t + 1)
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define nl cout << "\n"
#define sp cout << " "
#define F first
#define S second
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<pii>
#define vll vector<pll>
#define MOD 1000000007
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

void mod(ll &n, ll m = MOD) { n %= m, n += m, n %= m; }

const int MAXN = 1e5 + 5;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

vector<int> gr[MAXN];
bool vis[MAXN];
int dp[MAXN];

void dfs(int node, int par) {
  vis[node] = 1;
  int ans = 0;
  for (auto next : gr[node]) {
    dfs(next, node);
    ans = max(ans, dp[next]);
  }
  dp[node] = ans + 1;
}

int main() {
  fastio();

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    gr[u].pb(v);
  }

  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, -1);
      mx = max(mx, dp[i]);
    }
  }
  cout << mx - 1, nl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define test()                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  loop(test, 1, t + 1)
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define nl cout << "\n"
#define sp cout << " "
#define F first
#define S second
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<pii>
#define vll vector<pll>
#define MOD 1000000007
#define all(x) x.begin(), x.end()

template <class C> void min_self(C &a, C b) { a = min(a, b); }
template <class C> void max_self(C &a, C b) { a = max(a, b); }

void mod(ll &n, ll m = MOD) { n %= m, n += m, n %= m; }

const int MAXN = 1e5 + 5;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

vector<int> gr[MAXN];
bool vis[MAXN];
int dp[MAXN];

void dfs(int node, int par) {
  vis[node] = 1;
  int ans = 0;
  for (auto next : gr[node]) {
    if (!vis[next])
      dfs(next, node);
    ans = max(ans, dp[next]);
  }
  dp[node] = ans + 1;
}

int main() {
  fastio();

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    gr[u].pb(v);
  }

  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dfs(i, -1);
      mx = max(mx, dp[i]);
    }
  }
  cout << mx - 1, nl;
  return 0;
}