Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
/// Code By t_bone_289 (Tushar Singh)
/// An Immature Programmer.... I'm still developing !!
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef long long ll;
typedef double d;
typedef long double ld;
template <typename T> void ContainerError(T &v) {
  cout << "{";
  for (auto &x : v) {
    cout << x << ',';
  }
  cout << "}" << endl;
  return;
}
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
ll powmod(ll base, ll exp, ll MOD) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return (res % MOD);
}
ll mpow(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return (res);
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// dfs(i,G,dp,vis);

void dfs(ll ver, vector<vector<ll>> &G, vector<ll> &dp, vector<ll> &vis) {
  if (vis[ver]) {
    return;
  }
  for (ll j = 0; j < G[ver].size(); j++) {
    ll v = G[ver][j];
    dfs(v, G, dp, vis);
    dp[ver] = max(dp[v] + 1, dp[ver]);
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> G(n);
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
  }
  vector<ll> dp(n, 0);
  vector<ll> vis(n, 0);
  for (ll i = 0; i < n; i++) {
    dfs(i, G, dp, vis);
  }
  ll ans = dp[0];
  for (auto x : dp) {
    ans = max(ans, x);
  }
  cout << ans;
  return 0;
}


Fixed submission:
/// Code By t_bone_289 (Tushar Singh)
/// An Immature Programmer.... I'm still developing !!
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef long long ll;
typedef double d;
typedef long double ld;
template <typename T> void ContainerError(T &v) {
  cout << "{";
  for (auto &x : v) {
    cout << x << ',';
  }
  cout << "}" << endl;
  return;
}
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
ll powmod(ll base, ll exp, ll MOD) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return (res % MOD);
}
ll mpow(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return (res);
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// dfs(i,G,dp,vis);

void dfs(ll ver, vector<vector<ll>> &G, vector<ll> &dp, vector<ll> &vis) {
  if (vis[ver]) {
    return;
  }
  vis[ver] = 1;
  for (ll j = 0; j < G[ver].size(); j++) {
    ll v = G[ver][j];
    dfs(v, G, dp, vis);
    dp[ver] = max(dp[v] + 1, dp[ver]);
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, m;
  cin >> n >> m;
  vector<vector<ll>> G(n);
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
  }
  vector<ll> dp(n, 0);
  vector<ll> vis(n, 0);
  for (ll i = 0; i < n; i++) {
    dfs(i, G, dp, vis);
  }
  ll ans = dp[0];
  for (auto x : dp) {
    ans = max(ans, x);
  }
  cout << ans;
  return 0;
}