Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
#define M 1000000007
#define pb push_back
#define p_q priority_queue
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define endl '\n'
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a, n, b)                                                           \
  for (ll int i = 0; i < n; i++)                                               \
    a[i] = 0;
#define cset(a) __builtin_popcountll(a)
#define hell (ull)1e9
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << endl;                                                              \
  }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define re resize
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "   ";
  err(++it, args...);
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char ch) { return string("'") + ch + string("'"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class InputIterator>
string to_string(InputIterator first, InputIterator last) {
  bool start = true;
  string res = "{";
  while (first != last) {
    if (!start) {
      res += ", ";
    }
    start = false;
    res += to_string(*first);
    ++first;
  }
  res += "}";
  return res;
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename A, typename B>
istream &operator>>(istream &input, pair<A, B> &x) {
  input >> x.F >> x.S;
  return input;
}
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}

int n, m;
vi adj[(ll)1e5 + 1];
bool vis[(ll)1e5 + 1];
int dp[(ll)1e5 + 1];
void dfs(int s) {
  vis[s] = 1;
  for (int i : adj[s])
    if (!vis[i])
      dfs(i);
  for (int i : adj[s])
    dp[s] = max(dp[s], dp[i]);
  dp[s]++;
}
signed main(void) {
  ios
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> m;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
  }
  rep(i, 1, n + 1) if (!vis[i]) dfs(i);
  int ans = 0;
  rep(i, 1, n + 1) ans = max(ans, dp[i]);
  cout << ans - 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
#define M 1000000007
#define pb push_back
#define p_q priority_queue
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define endl '\n'
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ini(a, n, b)                                                           \
  for (ll int i = 0; i < n; i++)                                               \
    a[i] = 0;
#define cset(a) __builtin_popcountll(a)
#define hell (ull)1e9
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << endl;                                                              \
  }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define re resize
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "   ";
  err(++it, args...);
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char ch) { return string("'") + ch + string("'"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class InputIterator>
string to_string(InputIterator first, InputIterator last) {
  bool start = true;
  string res = "{";
  while (first != last) {
    if (!start) {
      res += ", ";
    }
    start = false;
    res += to_string(*first);
    ++first;
  }
  res += "}";
  return res;
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename A, typename B>
istream &operator>>(istream &input, pair<A, B> &x) {
  input >> x.F >> x.S;
  return input;
}
template <typename A> istream &operator>>(istream &input, vector<A> &x) {
  for (auto &i : x)
    input >> i;
  return input;
}

int n, m;
vi adj[(ll)1e5 + 1];
bool vis[(ll)1e5 + 1];
int dp[(ll)1e5 + 1];
void dfs(int s) {
  vis[s] = 1;
  for (int i : adj[s])
    if (!vis[i])
      dfs(i);
  for (int i : adj[s])
    dp[s] = max(dp[s], dp[i]);
  dp[s]++;
}
signed main(void) {
  ios cin >> n >> m;
  rep(i, 0, m) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
  }
  rep(i, 1, n + 1) if (!vis[i]) dfs(i);
  int ans = 0;
  rep(i, 1, n + 1) ans = max(ans, dp[i]);
  cout << ans - 1 << endl;
}