Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll, ll>
#define mod 1000000007
#define pll pair<ll, ll>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define prll(a, n)                                                             \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << a[i] << " ";                                                       \
  }
#define ain(a)                                                                 \
  for (auto &i : a)                                                            \
  cin >> i
#define mms(a) memset(a, 0, sizeof(a))
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

ll max_ll(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll min_ll(ll a, ll b, ll c) { return min(a, min(b, c)); }

bool sortBySec(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return (a.second < b.second);
}

long long binexpmod(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return (res);
}
const int nix = 6;
vi edges[nix];
vi in_degree(nix);
vi vis(nix);
vi dist(nix, 0);

void dfs(ll i) {
  assert(!vis[i]);
  vis[i] = 1;
  for (auto e : edges[i]) {
    dist[e] = max(dist[e], dist[i] + 1);
    in_degree[e]--;
    if (in_degree[e] == 0) {
      dfs(e);
    }
  }
  return;
}
ll lcm(ll a, ll b) {
  ll x = __gcd(a, b);
  a /= x;
  b /= x;
  ll sum = a * b * x;
  return sum;
}
void solve() {
  ll n, m, res = 0;
  cin >> n >> m;
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    edges[x].push_back(y);
    in_degree[y]++;
  }
  rep1(i, n) {
    if (!vis[i] && in_degree[i] == 0) {
      dfs(i);
    }
  }
  for (ll i : dist) {
    res = max(res, i);
  }
  cout << res << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll, ll>
#define mod 1000000007
#define pll pair<ll, ll>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define prll(a, n)                                                             \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << a[i] << " ";                                                       \
  }
#define ain(a)                                                                 \
  for (auto &i : a)                                                            \
  cin >> i
#define mms(a) memset(a, 0, sizeof(a))
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

ll max_ll(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll min_ll(ll a, ll b, ll c) { return min(a, min(b, c)); }

bool sortBySec(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return (a.second < b.second);
}

long long binexpmod(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return (res);
}
const int nix = 100005;
vi edges[nix];
vi in_degree(nix);
vi vis(nix);
vi dist(nix, 0);

void dfs(ll i) {
  assert(!vis[i]);
  vis[i] = 1;
  for (auto e : edges[i]) {
    dist[e] = max(dist[e], dist[i] + 1);
    in_degree[e]--;
    if (in_degree[e] == 0) {
      dfs(e);
    }
  }
  return;
}
ll lcm(ll a, ll b) {
  ll x = __gcd(a, b);
  a /= x;
  b /= x;
  ll sum = a * b * x;
  return sum;
}
void solve() {
  ll n, m, res = 0;
  cin >> n >> m;
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    edges[x].push_back(y);
    in_degree[y]++;
  }
  rep1(i, n) {
    if (!vis[i] && in_degree[i] == 0) {
      dfs(i);
    }
  }
  for (ll i : dist) {
    res = max(res, i);
  }
  cout << res << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}