Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define LL long long
#define ull unsigned ll
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()

template <typename T> inline void smin(T &a, T b) { a = a < b ? a : b; }
template <typename T> inline void smax(T &a, T b) { a = a > b ? a : b; }

template <typename T> inline void Int(T &n) {
  n = 0;
  int f = 1;
  register int ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = (n << 3) + (n << 1) + ch - '0';
  n *= f;
}
template <typename T, typename TT> inline void Int(T &n, TT &m) {
  Int(n), Int(m);
}
template <typename T, typename TT, typename TTT>
inline void Int(T &n, TT &m, TTT &l) {
  Int(n), Int(m, l);
}

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(all(_s), ',', ' ');                                                \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}

const int mod = (int)1e9 + 7;

int add(int a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
int sub(int a, int b) {
  a -= b;
  if (a <= 0)
    a += mod;
  return a;
}
int mul(int a, int b) { return (ll)a * b % mod; }

const int inf = (int)1e9 + 5;
const ll Inf = (ll)2e18 + 5;
const int N = (int)3e3 + 5;

vector<int> g[N];
int dp[N], vis[N];

void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    if (!vis[v])
      dfs(v);
    smax(dp[u], dp[v] + 1);
  }
}

int solve() {
  int n, m;
  Int(n, m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    Int(u, v);
    g[u].push_back(v);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    smax(ans, dp[i]);
  }
  printf("%d\n", ans);
  return 0;
}

int main() {
  int test = 1, tc = 0;
  // Int(test);
  while (test--) {
    // printf("Case %d: ", ++tc);
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define LL long long
#define ull unsigned ll
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define ff first
#define ss second
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()

template <typename T> inline void smin(T &a, T b) { a = a < b ? a : b; }
template <typename T> inline void smax(T &a, T b) { a = a > b ? a : b; }

template <typename T> inline void Int(T &n) {
  n = 0;
  int f = 1;
  register int ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = (n << 3) + (n << 1) + ch - '0';
  n *= f;
}
template <typename T, typename TT> inline void Int(T &n, TT &m) {
  Int(n), Int(m);
}
template <typename T, typename TT, typename TTT>
inline void Int(T &n, TT &m, TTT &l) {
  Int(n), Int(m, l);
}

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(all(_s), ',', ' ');                                                \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}

const int mod = (int)1e9 + 7;

int add(int a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
int sub(int a, int b) {
  a -= b;
  if (a <= 0)
    a += mod;
  return a;
}
int mul(int a, int b) { return (ll)a * b % mod; }

const int inf = (int)1e9 + 5;
const ll Inf = (ll)2e18 + 5;
const int N = (int)3e5 + 5;

vector<int> g[N];
int dp[N], vis[N];

void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u]) {
    if (!vis[v])
      dfs(v);
    smax(dp[u], dp[v] + 1);
  }
}

int solve() {
  int n, m;
  Int(n, m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    Int(u, v);
    g[u].push_back(v);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    smax(ans, dp[i]);
  }
  printf("%d\n", ans);
  return 0;
}

int main() {
  int test = 1, tc = 0;
  // Int(test);
  while (test--) {
    // printf("Case %d: ", ++tc);
    solve();
  }
  return 0;
}