Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define N 100009
#define INF 1000000009
#define MOD 1000000007
typedef long long int ll;
#define sd(x) scanf("%d", &x)
#define sd2(x, y) scanf("%d%d", &x, &y)
#define sd3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define pf(x) printf("%d", x);
#define pf2(x, y) printf("%d %d\n", x, y);
#define pf3(x, y, z) printf("%d %d %d\n", x, y, z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define tr(x) cout << x << endl;
#define tr2(x, y) cout << x << " " << y << " " << endl;
#define tr3(x, y, z) cout << x << " " << y << " " << z << endl;
#define tr4(x, y, z, a)                                                        \
  cout << x << " " << y << " " << z << " " << a << " " << endl;
int a[N], b[N], t;
vector<int> v[N], temp;
bool visit1[N], visit2[N];
void dfs1(int i) {
  visit1[i] = 1;
  for (auto j : v[i]) {
    if (!visit1[j])
      dfs1(j);
  }
  // tr(i);
  temp.pb(i);
}
int ans = 0;
void dfs2(int i) {
  visit2[i] = 1;
  for (auto j : v[i]) {
    dfs2(j);
    a[i] = max(a[i], a[j] + 1);
  }
  ans = max(ans, a[i]);
}
int main() {
  int i, j, n, m, t, k, x, y;
  sd2(n, m);
  for (i = 0; i < m; i++) {
    sd2(x, y);
    v[x].push_back(y);
  }
  for (i = 1; i <= n; i++) {
    if (!visit1[i]) {
      dfs1(i);
    }
  }
  reverse(temp.begin(), temp.end());
  for (auto i : temp) {
    if (!visit2[i]) {
      dfs2(i);
    }
  }
  tr(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100009
#define INF 1000000009
#define MOD 1000000007
typedef long long int ll;
#define sd(x) scanf("%d", &x)
#define sd2(x, y) scanf("%d%d", &x, &y)
#define sd3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define pf(x) printf("%d", x);
#define pf2(x, y) printf("%d %d\n", x, y);
#define pf3(x, y, z) printf("%d %d %d\n", x, y, z);
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define _                                                                      \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define tr(x) cout << x << endl;
#define tr2(x, y) cout << x << " " << y << " " << endl;
#define tr3(x, y, z) cout << x << " " << y << " " << z << endl;
#define tr4(x, y, z, a)                                                        \
  cout << x << " " << y << " " << z << " " << a << " " << endl;
int a[N], b[N], t;
vector<int> v[N], temp;
bool visit1[N], visit2[N];
void dfs1(int i) {
  visit1[i] = 1;
  for (auto j : v[i]) {
    if (!visit1[j])
      dfs1(j);
  }
  // tr(i);
  temp.pb(i);
}
int ans = 0;
void dfs2(int i) {
  visit2[i] = 1;
  for (auto j : v[i]) {
    if (!visit2[j])
      dfs2(j);
    a[i] = max(a[i], a[j] + 1);
  }
  ans = max(ans, a[i]);
}
int main() {
  int i, j, n, m, t, k, x, y;
  sd2(n, m);
  for (i = 0; i < m; i++) {
    sd2(x, y);
    v[x].push_back(y);
  }
  for (i = 1; i <= n; i++) {
    if (!visit1[i]) {
      dfs1(i);
    }
  }
  reverse(temp.begin(), temp.end());
  for (auto i : temp) {
    if (!visit2[i]) {
      dfs2(i);
    }
  }
  tr(ans);
}