Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 1000000007
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

vector<vi> adj;
vi vis, ans;

void dfs(int root) {
  vis[root] = 1;
  for (auto i : adj[root]) {
    if (!vis[i])
      dfs(i);
    ans[root] = max(ans[root], ans[i] + 1);
  }
}

int32_t main() {
  fastIO inp_out();
  int n, m;
  cin >> n >> m;
  adj = vector<vi>(n + 1);
  vis = ans = vi(n + 1);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
  }

  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      dfs(i);
  int fa = 0;
  for (int i = 1; i <= n; ++i)
    fa = max(fa, ans[i]);
  cout << fa;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 1000000007
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

vector<vi> adj;
vi vis, ans;

void dfs(int root) {
  vis[root] = 1;
  for (auto i : adj[root]) {
    if (!vis[i])
      dfs(i);
    ans[root] = max(ans[root], ans[i] + 1);
  }
}

int32_t main() {
  fastIO
      // inp_out();
      int n,
      m;
  cin >> n >> m;
  adj = vector<vi>(n + 1);
  vis = ans = vi(n + 1);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
  }

  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      dfs(i);
  int fa = 0;
  for (int i = 1; i <= n; ++i)
    fa = max(fa, ans[i]);
  cout << fa;

  return 0;
}