Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define PI 3.1415926535897932384626433832795
int MOD = 1e9 + 7;
int powmod(int a, int l, int md) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a % md;
    l /= 2;
    a = a * a % md;
  }
  return res;
}
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) { return b | (1 << i); } // set ith bit
int __unset(int b, int i) { return b & !(1 << i); }
int __check(int b, int i) { return b & (1 << i); } // returns 0 if ith bit is 0
int mul(int a, int b) { return ((a % MOD) * (b % MOD)) % MOD; }
int add(int a, int b) { return (a + b) % MOD; }
int sub(int a, int b) { return (((a - b) % MOD) + MOD) % MOD; }
int __div(int a, int b) {
  return mul(a, powmod(b, MOD - 2, MOD));
} // if MOD is prime;
const int inf = 0xFFFFFFFFFFFFFFFL;                // very large number
priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
clock_t time_p = clock();
void time() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
int n, m;
vi vis(100010, 0);
int dp[100010];
int dfs(int v, vector<vi> adjlist) {
  vis[v] = 1;
  // if(!adjlist[v].size())
  // 	return 0;
  int mx = 0;
  for (int i = 0; i < adjlist[v].size(); i++) {
    int u = adjlist[v][i];
    // cout<<u<<" fefe "<<v<<" "<<vis[u]<<" "<<vis[v]<<endl;
    if (vis[u]) {
      // cout<<"fe "<<dp[u]<<endl;
      mx = max(mx, dp[u] + 1);
    } else
      mx = max(mx, dfs(u, adjlist) + 1);
  }
  dp[v] = mx;
  // cout<<mx<<" "<<v<<endl;
  return dp[v];
}
int32_t main(void) {
  IOS;
  cin >> n >> m;
  vector<vi> adjlist(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adjlist[u - 1].pb(v - 1);
  }
  for (int i = 0; i < n; i++)
    dp[i] = -1;
  for (int i = 0; i < n; i++) {
    if (!adjlist[i].size()) {
      dp[i] = 0;
      vis[i] = 1;
    }
  }
  // cout<<dp[3]<<endl;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dp[i] = dfs(i, adjlist);
    }
  }
  cout << *max_element(dp, dp + n) << endl;
  time();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define PI 3.1415926535897932384626433832795
int MOD = 1e9 + 7;
int powmod(int a, int l, int md) {
  int res = 1;
  while (l) {
    if (l & 1)
      res = res * a % md;
    l /= 2;
    a = a * a % md;
  }
  return res;
}
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
#define pb push_back
int __set(int b, int i) { return b | (1 << i); } // set ith bit
int __unset(int b, int i) { return b & !(1 << i); }
int __check(int b, int i) { return b & (1 << i); } // returns 0 if ith bit is 0
int mul(int a, int b) { return ((a % MOD) * (b % MOD)) % MOD; }
int add(int a, int b) { return (a + b) % MOD; }
int sub(int a, int b) { return (((a - b) % MOD) + MOD) % MOD; }
int __div(int a, int b) {
  return mul(a, powmod(b, MOD - 2, MOD));
} // if MOD is prime;
const int inf = 0xFFFFFFFFFFFFFFFL;                // very large number
priority_queue<int, vector<int>, greater<int>> pq; // for min priority_queue
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
clock_t time_p = clock();
void time() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
int n, m;
vi vis(100010, 0);
int dp[100010];
int dfs(int v, vector<vi> &adjlist) {
  vis[v] = 1;
  // if(!adjlist[v].size())
  // 	return 0;
  int mx = 0;
  for (int i = 0; i < adjlist[v].size(); i++) {
    int u = adjlist[v][i];
    // cout<<u<<" fefe "<<v<<" "<<vis[u]<<" "<<vis[v]<<endl;
    if (vis[u]) {
      // cout<<"fe "<<dp[u]<<endl;
      mx = max(mx, dp[u] + 1);
    } else
      mx = max(mx, dfs(u, adjlist) + 1);
  }
  dp[v] = mx;
  // cout<<mx<<" "<<v<<endl;
  return dp[v];
}
int32_t main(void) {
  IOS;
  cin >> n >> m;
  vector<vi> adjlist(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adjlist[u - 1].pb(v - 1);
  }
  for (int i = 0; i < n; i++)
    dp[i] = -1;
  for (int i = 0; i < n; i++) {
    if (!adjlist[i].size()) {
      dp[i] = 0;
      vis[i] = 1;
    }
  }
  // cout<<dp[3]<<endl;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      dp[i] = dfs(i, adjlist);
    }
  }
  cout << *max_element(dp, dp + n) << endl;
  time();
  return 0;
}