Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define repi(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define repdi(i, a) for (auto i = a.rbegin(); i != a.rend(); ++i)
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define MOD 1000000007
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl "\n"
#define INF 1e18
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define mod 998244353
#define limit 1000
#define fl(a, x) memset(a, x, sizeof(a));
#define pt(a, x) cout << get<x>(a) << " ";

#define int long
typedef vector<int> vii;
typedef pair<ll, ll> pii;
typedef vector<pii> vpi;
typedef vector<vector<ll>> vv;

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int n, m, dp[100001];
vector<int> g[100001];

int ans(int node) {
  int mx = 0;
  if (dp[node] != -1)
    return dp[node];
  for (auto i : g[node]) {
    mx = max(mx, 1 + ans(i));
  }

  return mx;
}

void solve() {
  cin >> n >> m;
  fl(dp, -1);
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    g[x].eb(y);
  }
  int mx = 0;
  rep(i, 1, n + 1) { mx = max(mx, ans(i)); }
  cout << mx << nl;
}

int32_t main() {

  fast;
  int t = 1;
  // cin>>t;
  rep(i, 0, t) { solve(); }

  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define repi(i, a) for (auto i = a.begin(); i != a.end(); ++i)
#define repdi(i, a) for (auto i = a.rbegin(); i != a.rend(); ++i)
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define MOD 1000000007
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define nl "\n"
#define INF 1e18
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define mod 998244353
#define limit 1000
#define fl(a, x) memset(a, x, sizeof(a));
#define pt(a, x) cout << get<x>(a) << " ";

#define int long
typedef vector<int> vii;
typedef pair<ll, ll> pii;
typedef vector<pii> vpi;
typedef vector<vector<ll>> vv;

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int n, m, dp[100001];
vector<int> g[100001];

int ans(int node) {
  int mx = 0;
  if (dp[node] != -1)
    return dp[node];
  for (auto i : g[node]) {
    mx = max(mx, 1 + ans(i));
  }

  return dp[node] = mx;
}

void solve() {
  cin >> n >> m;
  fl(dp, -1);
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    g[x].eb(y);
  }
  int mx = 0;
  rep(i, 1, n + 1) { mx = max(mx, ans(i)); }
  cout << mx << nl;
}

int32_t main() {

  fast;
  int t = 1;
  // cin>>t;
  rep(i, 0, t) { solve(); }

  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}