Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define mod(n) n % 1000000007
#define sp << " "
#define precision(n) cout << fixed << setprecision(n);
#define startTime                                                              \
  time_t start, end;                                                           \
  time(&start);
#define endTime                                                                \
  time(&end);                                                                  \
  double tt = double(end - start);                                             \
  cout << "Time taken : " << fixed << tt << setprecision(5);                   \
  cout << " sec" << endl;

using namespace std;

ll dp[100001];
ll color[100001];

void dfsVisit(vll graph[], ll u) {
  color[u] = 1;
  if (graph[u].empty()) {
    dp[u] = 0;
    return;
  }
  for (auto x : graph[u]) {
    dfsVisit(graph, x);
    dp[u] = max(dp[u], 1 + dp[x]);
  }
  return;
}

void dfs(vll graph[], ll n) {
  for (int i = 0; i < n; i++) {
    if (color[i] != 1)
      dfsVisit(graph, i);
  }
}

int main() {
  IOS;
  ll n, m, u, v;
  cin >> n >> m;
  vll graph[n];
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    graph[--u].pb(--v);
  }
  dfs(graph, n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define mod(n) n % 1000000007
#define sp << " "
#define precision(n) cout << fixed << setprecision(n);
#define startTime                                                              \
  time_t start, end;                                                           \
  time(&start);
#define endTime                                                                \
  time(&end);                                                                  \
  double tt = double(end - start);                                             \
  cout << "Time taken : " << fixed << tt << setprecision(5);                   \
  cout << " sec" << endl;

using namespace std;

ll dp[100001];
ll color[100001];

void dfsVisit(vll graph[], ll u) {
  color[u] = 1;
  if (graph[u].empty()) {
    dp[u] = 0;
    return;
  }
  for (auto x : graph[u]) {
    if (color[x] == 0)
      dfsVisit(graph, x);
    dp[u] = max(dp[u], 1 + dp[x]);
  }
  return;
}

void dfs(vll graph[], ll n) {
  for (int i = 0; i < n; i++) {
    if (color[i] != 1)
      dfsVisit(graph, i);
  }
}

int main() {
  IOS;
  ll n, m, u, v;
  cin >> n >> m;
  vll graph[n];
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    graph[--u].pb(--v);
  }
  dfs(graph, n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}