Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define make_it_fast                                                           \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define endl "\n"
#define ff first
#define ss second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}

template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}

void _print() { cerr << "]\n"; }

template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...) 20
#endif

ll power(ll a, ll b, ll m = 1e9 + 7) {
  a %= m;
  if (b == 1)
    return a;
  if (b == 0)
    return 1;
  ll ret = power(a, b / 2);
  ret = (ret % m * ret % m) % m;
  if (b & 1)
    ret = (ret % m * a % m) % m;
  return ret;
}

ll lcm(ll a, ll b) { return (a * b) / (__gcd(a, b)); }

const int N = 2e5;
ll dp[N], vis[N];
vector<ll> e[N];

ll dfs(ll u) {
  vis[u] = 1;
  for (auto k : e[u]) {
    ll ans = 0;
    ans = dfs(k);
    dp[u] = max(dp[u], ans + 1);
  }
  return dp[u];
}

void solve() {
  ll n, m, i;
  cin >> n >> m;
  memset(dp, 0, sizeof(dp));
  memset(vis, 0, sizeof(vis));
  for (i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    e[u].pb(v);
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  ll ans = -1;
  for (i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

int main() {
  make_it_fast;
  int TEST_CASES = 1;
  // cin>>TEST_CASES;
  while (TEST_CASES--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define make_it_fast                                                           \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define endl "\n"
#define ff first
#define ss second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}

template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}

void _print() { cerr << "]\n"; }

template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...) 20
#endif

ll power(ll a, ll b, ll m = 1e9 + 7) {
  a %= m;
  if (b == 1)
    return a;
  if (b == 0)
    return 1;
  ll ret = power(a, b / 2);
  ret = (ret % m * ret % m) % m;
  if (b & 1)
    ret = (ret % m * a % m) % m;
  return ret;
}

ll lcm(ll a, ll b) { return (a * b) / (__gcd(a, b)); }

const int N = 2e5;
ll dp[N], vis[N];
vector<ll> e[N];

ll dfs(ll u) {
  vis[u] = 1;
  for (auto k : e[u]) {
    if (dp[k]) {
      dp[u] = max(dp[u], dp[k] + 1);
      continue;
    }
    ll ans = 0;
    ans = dfs(k);
    dp[u] = max(dp[u], ans + 1);
  }
  return dp[u];
}

void solve() {
  ll n, m, i;
  cin >> n >> m;
  memset(dp, 0, sizeof(dp));
  memset(vis, 0, sizeof(vis));
  for (i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    e[u].pb(v);
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  ll ans = -1;
  for (i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

int main() {
  make_it_fast;
  int TEST_CASES = 1;
  // cin>>TEST_CASES;
  while (TEST_CASES--) {
    solve();
  }
  return 0;
}