Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define m_p make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a, i >= b; i--)
#define sz(x) (int)x.size()
#define F first
#define S second
#define debug(x)                                                               \
  for (int i = 0; i < x; i++)                                                  \
  cout << "##########" << endl
#define M 1000000007
#define ll long long int
#define lMax LLONG_MAX
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define vi vector<int>
#define vii vector<pii>

// queue<int> , prefix_sum(a,a+n) , reverse(a,a+n) , priority_queue <int> max
// heap , priority_queue <int, vector<int>, greater<int> > min heap;
// == > != precedence gretaer then & , << precedence greater than <= == => ,
// int chk, cnt, fl, pos, in, src, tar, val, res. dummy;  fill
// (myvector.begin(),myvector.begin()+4,5), freq;

vi edge[100001];
int vis[100001] = {0}, ans;
int dp[100001] = {0}, x[100001] = {0};
void dfs(int root) {
  if (edge[root].size() == 0)
    dp[root] = 0;

  vi::iterator itr;

  for (itr = edge[root].begin(); itr != edge[root].end(); itr++) {
    if (!vis[*itr])
      dfs(*itr);
    dp[root] = max(dp[root], dp[*itr] + 1);
    ans = max(ans, dp[root]);
  }
}

void solve() {
  int m, n;
  cin >> n >> m;

  while (m--) {
    int a, b;
    cin >> a >> b;
    edge[a].pb(b);
    x[b]++;
  }
  rep(i, 1, n + 1) {
    if (x[i] == 0)
      dfs(i);
  }

  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define m_p make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a, i >= b; i--)
#define sz(x) (int)x.size()
#define F first
#define S second
#define debug(x)                                                               \
  for (int i = 0; i < x; i++)                                                  \
  cout << "##########" << endl
#define M 1000000007
#define ll long long int
#define lMax LLONG_MAX
#define pii pair<int, int>
#define all(a) a.begin(), a.end()
#define vi vector<int>
#define vii vector<pii>

// queue<int> , prefix_sum(a,a+n) , reverse(a,a+n) , priority_queue <int> max
// heap , priority_queue <int, vector<int>, greater<int> > min heap;
// == > != precedence gretaer then & , << precedence greater than <= == => ,
// int chk, cnt, fl, pos, in, src, tar, val, res. dummy;  fill
// (myvector.begin(),myvector.begin()+4,5), freq;

vi edge[100001];
int vis[100001] = {0}, ans;
int dp[100001] = {0}, x[100001] = {0};
void dfs(int root) {
  if (edge[root].size() == 0)
    dp[root] = 0;

  vi::iterator itr;

  for (itr = edge[root].begin(); itr != edge[root].end(); itr++) {
    if (!vis[*itr])
      dfs(*itr);
    dp[root] = max(dp[root], dp[*itr] + 1);
    ans = max(ans, dp[root]);
  }
  vis[root] = 1;
}

void solve() {
  int m, n;
  cin >> n >> m;

  while (m--) {
    int a, b;
    cin >> a >> b;
    edge[a].pb(b);
    x[b]++;
  }
  rep(i, 1, n + 1) {
    if (x[i] == 0)
      dfs(i);
  }

  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}