Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// È¥°É£¡Æ¤¿¨Çð! °ÑAC´ø»ØÀ´£¡
//      ¤Ø¡¡¡¡¡¡¡¡¡¡£¯|
// ¡¡¡¡/£Ü7¡¡¡¡¡¡ ¡Ï£ß/
// ¡¡ /¡¡©¦¡¡¡¡ £¯¡¡£¯								AC		AC
// AC		AC
//    ©¦¡¡Z £ß,£¼¡¡£¯¡¡¡¡ /`©c						AC		AC		AC
//    AC ©¦¡¡¡¡¡¡¡¡¡¡©c¡¡¡¡ /¡¡¡¡¡µ					AC
//    AC		AC		AC
// ¡¡Y¡¡¡¡¡¡¡¡¡¡`¡¡ /¡¡¡¡/  /						AC		AC
// AC		AC
// ¡¡Øé¡ñ¡¡.¡¡¡ñ¡¡¡¡¡´¡¡/¡¡/						AC		AC
// AC		AC
// ¡¡()¡¡ ¤Ø¡¡¡¡¡¡¡¡|¡¡£Ü¡´							AC		AC
// AC		AC
// ¡¡¡¡>- ._¡¡ ¥£¡¡ ©¦ £¯£¯							AC		AC
// AC		AC
// ¡¡ / ¤Ø¡¡¡¡ /¡¡/£¼| £Ü£Ü							AC		AC
// AC		AC
// ¡¡ ©c_/¡¡¡¡(_£¯¡¡ ©¦£¯£¯							AC		AC
// AC		AC
// ¡¡¡¡ 7¡¡¡¡¡¡¡¡¡¡¡¡¡¡|£¯							AC		AC
// AC		AC
// ¡¡¡¡ £¾¨Dr£þ£þ`-¨D£ß/							AC		AC
// AC		AC
//**************************************Accepted*****************************************//

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define Unique(x) x.erase(unique(all(x)), x.end());

#define CIN_FILE "input.txt"
#define COUT_FILE "output.txt"

#define Pig (OO)

using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int Days[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline int read() {
#define gc getchar
  int ans = 0;
  bool f = 1;
  char ch = gc();

  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();

  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();

  return f ? ans : -ans;

#undef gc
}

typedef long long ll;

int n, m;
vector<int> v[100010];
int dp[100010];
bool vis[100010];

const int INF = 117901063;

int DFS(int x) {
  if (vis[x])
    return dp[x];

  int ans = 0;

  for (int i = 0; i < v[x].size(); i++)
    ans = max(ans, DFS(v[x][i]) + 1);

  return dp[x] = ans;
}

int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;

    v[x].pb(y);
  }

  int ans = 0;

  for (int i = 1; i <= n; i++)
    ans = max(ans, DFS(i));

  cout << ans;

  // printf("Time used = %.12f",(double)(clock())/CLOCKS_PER_SEC);
  return 0;
}

/*  GOD BLESS  */


Fixed submission:
// È¥°É£¡Æ¤¿¨Çð! °ÑAC´ø»ØÀ´£¡
//      ¤Ø¡¡¡¡¡¡¡¡¡¡£¯|
// ¡¡¡¡/£Ü7¡¡¡¡¡¡ ¡Ï£ß/
// ¡¡ /¡¡©¦¡¡¡¡ £¯¡¡£¯								AC		AC
// AC		AC
//    ©¦¡¡Z £ß,£¼¡¡£¯¡¡¡¡ /`©c						AC		AC		AC
//    AC ©¦¡¡¡¡¡¡¡¡¡¡©c¡¡¡¡ /¡¡¡¡¡µ					AC
//    AC		AC		AC
// ¡¡Y¡¡¡¡¡¡¡¡¡¡`¡¡ /¡¡¡¡/  /						AC		AC
// AC		AC
// ¡¡Øé¡ñ¡¡.¡¡¡ñ¡¡¡¡¡´¡¡/¡¡/						AC		AC
// AC		AC
// ¡¡()¡¡ ¤Ø¡¡¡¡¡¡¡¡|¡¡£Ü¡´							AC		AC
// AC		AC
// ¡¡¡¡>- ._¡¡ ¥£¡¡ ©¦ £¯£¯							AC		AC
// AC		AC
// ¡¡ / ¤Ø¡¡¡¡ /¡¡/£¼| £Ü£Ü							AC		AC
// AC		AC
// ¡¡ ©c_/¡¡¡¡(_£¯¡¡ ©¦£¯£¯							AC		AC
// AC		AC
// ¡¡¡¡ 7¡¡¡¡¡¡¡¡¡¡¡¡¡¡|£¯							AC		AC
// AC		AC
// ¡¡¡¡ £¾¨Dr£þ£þ`-¨D£ß/							AC		AC
// AC		AC
//**************************************Accepted*****************************************//

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define Unique(x) x.erase(unique(all(x)), x.end());

#define CIN_FILE "input.txt"
#define COUT_FILE "output.txt"

#define Pig (OO)

using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int Days[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

inline int read() {
#define gc getchar
  int ans = 0;
  bool f = 1;
  char ch = gc();

  while (!isdigit(ch))
    f ^= ch == '-', ch = gc();

  while (isdigit(ch))
    ans = ((ans << 2) + ans << 1) + (ch ^ 48), ch = gc();

  return f ? ans : -ans;

#undef gc
}

typedef long long ll;

int n, m;
vector<int> v[100010];
int dp[100010];
bool vis[100010];

const int INF = 117901063;

int DFS(int x) {
  if (vis[x])
    return dp[x];

  vis[x] = 1;

  int ans = 0;

  for (int i = 0; i < v[x].size(); i++)
    ans = max(ans, DFS(v[x][i]) + 1);

  return dp[x] = ans;
}

int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;

    v[x].pb(y);
  }

  int ans = 0;

  for (int i = 1; i <= n; i++)
    ans = max(ans, DFS(i));

  cout << ans;

  // printf("Time used = %.12f",(double)(clock())/CLOCKS_PER_SEC);
  return 0;
}

/*  GOD BLESS  */