Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define LIM 100003
#define M 1000000007
#define inf 99999999999999999LL // long long inf
typedef long long ll;

#define sc(x) scanf("%d", &x);
#define sc2(x, y) scanf("%d%d", &x, &y);
#define sc3(x, y, z) scanf("%d%d%d", &x, &y, &z);
#define scl(x) scanf("%lld", &x);
#define scl2(x, y) scanf("%lld%lld", &x, &y);
#define scl3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);

#define pb push_back
#define mp make_pair
#define fi first
#define se second
ll power(ll x, ll y, ll z) {
  ll ret = 1;
  while (y > 0) {
    if (y & 1)
      ret = (ret * x) % z;
    x = (x * x) % z;
    y >>= 1;
  }
  return ret;
}

#define debug(x) cerr << #x << " :: " << x << "\n";
#define debug2(x, y)                                                           \
  cerr << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z)                                                        \
  cerr << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z         \
       << " :: " << z << "\n";
#define debug4(x, y, z, a)                                                     \
  cerr << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z         \
       << " :: " << z << "\t" << #a << " :: " << a << "\n";

int n, m, vis[LIM], dp[LIM], maxi;
vector<int> graph[LIM];
void dfs(int start) {
  dp[start] = 1;
  for (int ele : graph[start]) {
    if (vis[ele])
      continue;
    dfs(ele);
  }
  for (int ele : graph[start]) {
    dp[start] = max(dp[start], dp[ele] + 1);
  }
  maxi = max(maxi, dp[start] - 1);
}
int main() {
  int i, j, t;
  sc2(n, m);
  while (m--) {
    int st, en;
    sc2(st, en);
    graph[st].pb(en);
  }
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0)
      dfs(i);
  }
  cout << maxi << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define LIM 100003
#define M 1000000007
#define inf 99999999999999999LL // long long inf
typedef long long ll;

#define sc(x) scanf("%d", &x);
#define sc2(x, y) scanf("%d%d", &x, &y);
#define sc3(x, y, z) scanf("%d%d%d", &x, &y, &z);
#define scl(x) scanf("%lld", &x);
#define scl2(x, y) scanf("%lld%lld", &x, &y);
#define scl3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);

#define pb push_back
#define mp make_pair
#define fi first
#define se second
ll power(ll x, ll y, ll z) {
  ll ret = 1;
  while (y > 0) {
    if (y & 1)
      ret = (ret * x) % z;
    x = (x * x) % z;
    y >>= 1;
  }
  return ret;
}

#define debug(x) cerr << #x << " :: " << x << "\n";
#define debug2(x, y)                                                           \
  cerr << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z)                                                        \
  cerr << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z         \
       << " :: " << z << "\n";
#define debug4(x, y, z, a)                                                     \
  cerr << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z         \
       << " :: " << z << "\t" << #a << " :: " << a << "\n";

int n, m, vis[LIM], dp[LIM], maxi;
vector<int> graph[LIM];
void dfs(int start) {
  dp[start] = 1;
  vis[start] = 1;
  for (int ele : graph[start]) {
    if (vis[ele])
      continue;
    dfs(ele);
  }
  for (int ele : graph[start]) {
    dp[start] = max(dp[start], dp[ele] + 1);
  }
  maxi = max(maxi, dp[start] - 1);
}
int main() {
  int i, j, t;
  sc2(n, m);
  while (m--) {
    int st, en;
    sc2(st, en);
    graph[st].pb(en);
  }
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0)
      dfs(i);
  }
  cout << maxi << endl;

  return 0;
}