Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using boost::multiprecision::cpp_int;
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0)
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define reptArIn(i, a, b, ar)                                                  \
  for (i = a; i < b; ++i)                                                      \
    cin >> ar[i];
#define fo(i, a, b) for (i = a; i < b; ++i)
#define foe(i, a, b) for (i = a; i <= b; ++i)
#define rep(i, n) for (i = 1; i <= n; i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define ins insert
#define mk make_pair
#define f first
#define s second
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int> // typedef pair<int,int> pii;
#define pll pair<ll, ll>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
// mp.reserve(4096);
// mp.max_load_factor(0.25);

void dfs(int p, vector<int> adj[], int dp[], bool vis[]) {
  vis[p] = true;
  for (auto v : adj[p]) {
    dp[v] = max(dp[v], dp[p] + 1);
    dfs(v, adj, dp, vis);
  }
}
int main() {
  int n, m, i, x, y;
  cin >> n >> m;
  vector<int> adj[n + 1];
  int dp[n + 1];
  bool vis[n + 1];
  memset(vis, false, sizeof(vis));
  memset(dp, 0, sizeof(dp));
  foe(i, 1, m) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  foe(i, 1, n) {
    if (!vis[i])
      dfs(i, adj, dp, vis);
  }
  int mx = INT_MIN;
  foe(i, 1, n) { mx = max(mx, dp[i]); }
  cout << mx << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using boost::multiprecision::cpp_int;
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0)
#define ll long long int
#define ld long double
#define ull unsigned long long int
#define reptArIn(i, a, b, ar)                                                  \
  for (i = a; i < b; ++i)                                                      \
    cin >> ar[i];
#define fo(i, a, b) for (i = a; i < b; ++i)
#define foe(i, a, b) for (i = a; i <= b; ++i)
#define rep(i, n) for (i = 1; i <= n; i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define ins insert
#define mk make_pair
#define f first
#define s second
#define mod 1000000007
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int> // typedef pair<int,int> pii;
#define pll pair<ll, ll>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
// mp.reserve(4096);
// mp.max_load_factor(0.25);

void dfs(int p, vector<int> adj[], int dp[], bool vis[]) {
  vis[p] = true;
  for (auto v : adj[p]) {
    if (!vis[v])
      dfs(v, adj, dp, vis);

    dp[p] = max(1 + dp[v], dp[p]);
  }
}
int main() {
  int n, m, i, x, y;
  cin >> n >> m;
  vector<int> adj[n + 1];
  int dp[n + 1];
  bool vis[n + 1];
  memset(vis, false, sizeof(vis));
  memset(dp, 0, sizeof(dp));
  foe(i, 1, m) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  foe(i, 1, n) {
    if (!vis[i])
      dfs(i, adj, dp, vis);
  }
  int mx = INT_MIN;
  foe(i, 1, n) { mx = max(mx, dp[i]); }
  cout << mx << endl;
  return 0;
}