Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAX 2000005
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vp vector<pii>
#define all(v) v.begin(), v.end()
#define lb(v, x) lower_bound(all(v), x)
#define ub(v, x) upper_bound(all(v), x)
#define sz(x) (ll) x.size()
#define F first
#define S second
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ROF(i, a, b) for (ll i = a; i >= b; --i)
#define trace(x) cerr << #x << ": " << x << '\n';
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update>

ll mexp(ll a, ll b) {
  ll ans = 1;
  a %= MOD;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b >>= 1;
    a = a * a % MOD;
  }
  return ans;
}

ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return gcd(b % a, a);
}

const int N = 100005;

vi g[N];
ll dist[N];

ll dfs(int u) {
  if (dist[u] != -1) {
    return dist[u];
  }
  ll d = 0;
  for (int v : g[u]) {
    d = max(d, dfs(v));
  }
  d = d + 1;
  return d;
}

int main() {
  IOS
    
    FOR(i,0,N+1){
    dist[i] = -1;
  }
  int n, m;
  cin >> n >> m;
  FOR(i, 0, m) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
  }
  ll ans = 0;
  FOR(i, 1, n + 1) { ans = max(ans, dfs(i) - 1); }
  cout << ans;
  cerr << 1.0 / CLOCKS_PER_SEC << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAX 2000005
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vp vector<pii>
#define all(v) v.begin(), v.end()
#define lb(v, x) lower_bound(all(v), x)
#define ub(v, x) upper_bound(all(v), x)
#define sz(x) (ll) x.size()
#define F first
#define S second
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ROF(i, a, b) for (ll i = a; i >= b; --i)
#define trace(x) cerr << #x << ": " << x << '\n';
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update>

ll mexp(ll a, ll b) {
  ll ans = 1;
  a %= MOD;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b >>= 1;
    a = a * a % MOD;
  }
  return ans;
}

ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return gcd(b % a, a);
}

const int N = 100005;

vi g[N];
ll dist[N];

ll dfs(int u) {
  if (dist[u] != -1) {
    return dist[u];
  }
  ll d = 0;
  for (int v : g[u]) {
    d = max(d, dfs(v));
  }
  dist[u] = d + 1;
  return dist[u];
}

int main() {
  IOS
    
    FOR(i,0,N+1){
    dist[i] = -1;
  }
  int n, m;
  cin >> n >> m;
  FOR(i, 0, m) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
  }
  ll ans = 0;
  FOR(i, 1, n + 1) { ans = max(ans, dfs(i) - 1); }
  cout << ans;
  cerr << 1.0 / CLOCKS_PER_SEC << endl;
  return 0;
}