Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define fill(x, y) memset(x, y, sizeof(x))
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

vector<ll> memo;

ll dp(ll n, vvll links) {

  if (memo[n] != -1)
    return memo[n];
  ll res = 0;
  for (auto d : links[n]) {
    res = max(res, dp(d, links) + 1);
  }

  return memo[n] = res;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<ll>> links(N);
  int s, t;
  for (auto i = 0; i < M; i++) {
    cin >> s >> t;
    s--;
    t--;
    links[s].push_back(t);
  }

  memo.resize(N + 1, -1);

  ll ans = 0;

  for (auto i = 0; i < N; i++) {
    ans = max(ans, dp(i, links));
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define fill(x, y) memset(x, y, sizeof(x))
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

vector<ll> memo;

ll dp(ll n, vvll &links) {

  if (memo[n] != -1)
    return memo[n];
  ll res = 0;
  for (auto d : links[n]) {
    res = max(res, dp(d, links) + 1);
  }

  return memo[n] = res;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<vector<ll>> links(N);
  int s, t;
  for (auto i = 0; i < M; i++) {
    cin >> s >> t;
    s--;
    t--;
    links[s].push_back(t);
  }

  memo.resize(N + 1, -1);

  ll ans = 0;

  for (auto i = 0; i < N; i++) {
    ans = max(ans, dp(i, links));
  }

  cout << ans << endl;

  return 0;
}