Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
using namespace std;
#include <bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define vect vector<ll>
#define maap map<ll, ll>
#define MOD 1000000007
#define mit map::iterator
#define pii pair<ll, ll>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
typedef long long int ll;
// stringstream convert;
//   convert << number;
//   string s = convert.str();
ll fact[500005], invfact[500005];
ll power(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2 == 1)
      z = (z * x) % MOD;
    x = (x * x) % MOD;
    y /= 2;
  }
  return z;
}
ll inv(ll x) { return power(x, MOD - 2); }
ll divide(ll x, ll y) { return (x * inv(y)) % MOD; }
ll C(ll n, ll k) {
  if (k > n)
    return 0;
  return divide(fact[n], (fact[n - k] * fact[k]) % MOD);
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
bool isPrime(int n) {

  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
void init() {
  ll p = MOD;
  fact[0] = 1;
  ll i;
  for (i = 1; i < 500005; i++) {
    fact[i] = (i * fact[i - 1]) % p;
  }
  i--;
  invfact[i] = power(fact[i], p - 2);
  for (i--; i >= 0; i--) {
    invfact[i] = (invfact[i + 1] * (i + 1)) % p;
  }
}
ll s;
ll fun(ll a) {
  if (a == 0)
    return 0;
  return a + fun(a - 1);
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> gq;

#define MAXN 10000001
ll spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXN; i++)
    spf[i] = i;
  for (ll i = 4; i < MAXN; i += 2)
    spf[i] = 2;
  for (ll i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (ll j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
set<ll> getFactorization(ll x) {
  set<ll> ret;
  while (x != 1) {
    ret.insert(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
ll sm(ll n) {
  ll i;
  ll k = 0;
  while (n > 0) {
    k = k + n % 10;
    n = n / 10;
  }
  return k;
}
vector<ll> v[100001];
ll dp[100001];
ll vis[100001];
void dfs(ll k) {
  vis[k] = 1;
  for (ll i = 0; i < v[k].size(); i++) {
    dfs(v[k][i]);
    dp[k] = max(dp[k], 1 + dp[v[k][i]]);
  }
}
void ghost() {
  ll i = 0, j = 0, k = 0, l = 0, n, r = 0, q = 0, y = 100000000000, m, w;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> k >> l;
    v[k].pb(l);
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  ll ans = 0;
  for (i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}
signed main() {
  int test = 1;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fast
      // cin>>test;
      while (test--) ghost();
  return 0;
}


Fixed submission:
using namespace std;
#include <bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define vect vector<ll>
#define maap map<ll, ll>
#define MOD 1000000007
#define mit map::iterator
#define pii pair<ll, ll>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
typedef long long int ll;
// stringstream convert;
//   convert << number;
//   string s = convert.str();
ll fact[500005], invfact[500005];
ll power(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2 == 1)
      z = (z * x) % MOD;
    x = (x * x) % MOD;
    y /= 2;
  }
  return z;
}
ll inv(ll x) { return power(x, MOD - 2); }
ll divide(ll x, ll y) { return (x * inv(y)) % MOD; }
ll C(ll n, ll k) {
  if (k > n)
    return 0;
  return divide(fact[n], (fact[n - k] * fact[k]) % MOD);
}
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
bool isPrime(int n) {

  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}
void init() {
  ll p = MOD;
  fact[0] = 1;
  ll i;
  for (i = 1; i < 500005; i++) {
    fact[i] = (i * fact[i - 1]) % p;
  }
  i--;
  invfact[i] = power(fact[i], p - 2);
  for (i--; i >= 0; i--) {
    invfact[i] = (invfact[i + 1] * (i + 1)) % p;
  }
}
ll s;
ll fun(ll a) {
  if (a == 0)
    return 0;
  return a + fun(a - 1);
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> gq;

#define MAXN 10000001
ll spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (ll i = 2; i < MAXN; i++)
    spf[i] = i;
  for (ll i = 4; i < MAXN; i += 2)
    spf[i] = 2;
  for (ll i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (ll j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
set<ll> getFactorization(ll x) {
  set<ll> ret;
  while (x != 1) {
    ret.insert(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
ll sm(ll n) {
  ll i;
  ll k = 0;
  while (n > 0) {
    k = k + n % 10;
    n = n / 10;
  }
  return k;
}
vector<ll> v[100001];
ll dp[100001];
ll vis[100001];
void dfs(ll k) {
  vis[k] = 1;
  for (ll i = 0; i < v[k].size(); i++) {
    if (!vis[v[k][i]])
      dfs(v[k][i]);
    dp[k] = max(dp[k], 1 + dp[v[k][i]]);
  }
}
void ghost() {
  ll i = 0, j = 0, k = 0, l = 0, n, r = 0, q = 0, y = 100000000000, m, w;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> k >> l;
    v[k].pb(l);
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  ll ans = 0;
  for (i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}
signed main() {
  int test = 1;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  fast
      // cin>>test;
      while (test--) ghost();
  return 0;
}