Problem p03166 (C++)

Buggy submission (status: Runtime Error):
// simple dfs not enough coz constraints of order 10^5 are high for normal
// recursion
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mkp make_pair
#define pb push_back
#define inf INT_MAX
vector<int> g[10005];
vector<int> dp;
void pairsort(int a[], int b[], int n) {
  pair<int, int> pairt[n];
  for (int i = 0; i < n; i++) {
    pairt[i].first = a[i];
    pairt[i].second = b[i];
  }
  sort(pairt, pairt + n);
  for (int i = 0; i < n; i++) {
    a[i] = pairt[i].first;
    b[i] = pairt[i].second;
  }
}
int binomialCoeff(int n, int k) {
  int res = 1;

  // Since C(n, k) = C(n, n-k)
  if (k > n - k)
    k = n - k;

  // Calculate value of [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
  for (int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }

  return res;
}
void printVector(vector<ll> v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i];
  }
}
int divisor(int num) {
  int div = 0;
  for (int i = 1; i <= num; i++) {
    /* code */
    if (num % i == 0)
      div++;
  }
  return div;
}
int sub(int a, int b) {
  if (a >= b)
    return a - b;
  else
    return b - a;
}
// this is like a modified DFS
// dp array stores overlapping substructures results & hence also does work of
// visited

int solve(int a) {
  if (dp[a] != -1)
    return dp[a];
  int res = 0;
  for (int i = 0; i < g[a].size(); i++)
    res = max(res, solve(g[a][i]) + 1);
  return dp[a] = res;
}

int32_t main() {
  int n, m, x, y;
  cin >> n >> m;
  // dp[i]: stores max length in the graph starting from ith node
  dp.resize(n + 2, -1);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].pb(y);
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, solve(i));
  cout << ans;
  return 0;
}


Fixed submission:
// simple dfs not enough coz constraints of order 10^5 are high for normal
// recursion
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mkp make_pair
#define pb push_back
#define inf INT_MAX
vector<int> g[110005];
vector<int> dp;
void pairsort(int a[], int b[], int n) {
  pair<int, int> pairt[n];
  for (int i = 0; i < n; i++) {
    pairt[i].first = a[i];
    pairt[i].second = b[i];
  }
  sort(pairt, pairt + n);
  for (int i = 0; i < n; i++) {
    a[i] = pairt[i].first;
    b[i] = pairt[i].second;
  }
}
int binomialCoeff(int n, int k) {
  int res = 1;

  // Since C(n, k) = C(n, n-k)
  if (k > n - k)
    k = n - k;

  // Calculate value of [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
  for (int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }

  return res;
}
void printVector(vector<ll> v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i];
  }
}
int divisor(int num) {
  int div = 0;
  for (int i = 1; i <= num; i++) {
    /* code */
    if (num % i == 0)
      div++;
  }
  return div;
}
int sub(int a, int b) {
  if (a >= b)
    return a - b;
  else
    return b - a;
}
// this is like a modified DFS
// dp array stores overlapping substructures results & hence also does work of
// visited

int solve(int a) {
  if (dp[a] != -1)
    return dp[a];
  int res = 0;
  for (int i = 0; i < g[a].size(); i++)
    res = max(res, solve(g[a][i]) + 1);
  return dp[a] = res;
}

int32_t main() {
  int n, m, x, y;
  cin >> n >> m;
  // dp[i]: stores max length in the graph starting from ith node
  dp.resize(n + 2, -1);
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    g[x].pb(y);
  }

  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, solve(i));
  cout << ans;
  return 0;
}