Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define Art Code
#define Artist Shivam Gupta
#define MoT This Art belongs to Artist
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long int
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vl(p) vector<p>
#define W while
#define For(i, s, x) for (i = s; i < x; i++)
#define Fore(i, s, x) for (i = s; i <= x; i++)
#define FoR(i, s, x) for (i = s - 1; i >= x; i--)
#define ForE(i, s, x) for (i = s; i >= x; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define in insert
#define sz size()
#define F first
#define S second
#define nl cout << "\n"
#define pr(a) cout << a << " "
#define pr2(a, b) cout << a << " " << b << "\n"
#define pr3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define trace2(x, y)                                                           \
  cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define mod 1000000007
#define MAX 200005

vl(vll) out;
vll in, outs, ans;

void doit(ll n) {
  for (auto r : out[n]) {
    ans[r] = max(ans[r], ans[n] + 1);
    doit(r);
  }
}

int main() {
  FASTIO;
  ll t = 1, i, j, k, l;
  // cin>>t;
  W(t--) {
    ll n, m;
    cin >> n >> m;
    out.resize(n + 1);
    ans.resize(n + 1);
    in.resize(n + 1);
    For(i, 0, m) cin >> j >> k, out[j].pb(k), in[k]++;
    Fore(i, 1, n) if (!in[i]) outs.pb(i);
    for (auto r : outs)
      doit(r);
    pr(*max_element(all(ans)));
    nl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define Art Code
#define Artist Shivam Gupta
#define MoT This Art belongs to Artist
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long int
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vl(p) vector<p>
#define W while
#define For(i, s, x) for (i = s; i < x; i++)
#define Fore(i, s, x) for (i = s; i <= x; i++)
#define FoR(i, s, x) for (i = s - 1; i >= x; i--)
#define ForE(i, s, x) for (i = s; i >= x; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define in insert
#define sz size()
#define F first
#define S second
#define nl cout << "\n"
#define pr(a) cout << a << " "
#define pr2(a, b) cout << a << " " << b << "\n"
#define pr3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define trace2(x, y)                                                           \
  cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                                                        \
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \
       << z << endl;
#define trace4(a, b, c, d)                                                     \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)                                                  \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define mod 1000000007
#define MAX 200005

vl(vll) out;
vll in, outs, ans;

void doit(ll n) {
  for (auto r : out[n]) {
    ans[r] = max(ans[r], ans[n] + 1);
    in[r]--;
    if (!in[r])
      doit(r);
  }
}

int main() {
  FASTIO;
  ll t = 1, i, j, k, l;
  // cin>>t;
  W(t--) {
    ll n, m;
    cin >> n >> m;
    out.resize(n + 1);
    ans.resize(n + 1);
    in.resize(n + 1);
    For(i, 0, m) cin >> j >> k, out[j].pb(k), in[k]++;
    Fore(i, 1, n) if (!in[i]) outs.pb(i);
    for (auto r : outs)
      doit(r);
    pr(*max_element(all(ans)));
    nl;
  }
  return 0;
}