Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
#define endl "\n"
#define FILEIO                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define LL long long
#define mod 1000000007LL
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define pp pop_back
// #define p push
#define I insert
#define bg begin()
#define ed end()
#define sz size()
#define all(v) v.begin(), v.end()
typedef vector<LL> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef map<LL, LL> mii;
// typedef priority_queue<int> pq;
typedef queue<int> que;
typedef stack<int> stk;
#define Inf 100000005LL
#define f first
#define s second

vi vect_2pow;
void pow_of_2() {
  vect_2pow.assign(32, 0);
  for (int i = 0; i <= 32; i++) {
    LL y = 1 << i;
    vect_2pow[i] = y;
  }
}
bool Ispalindrome(string str) {
  LL len = str.size();
  bool flag;
  for (int i = 0; i < len / 2; i++) {
    if (str[i] == str[len - i - 1]) {
      flag = 0;
      continue;
    } else {
      flag = 1;
      break;
    }
  }
  if (flag == 1)
    return false;
  else
    return true;
}
vector<LL> prime_check;
mii get_high_prime;
int high_prime_factor() {
  // cout<<"ecd";
  LL max_prime = 0;
  for (int i = 2; i < 100000; i++) {
    LL n = i;

    while (n % 2 == 0) {
      max_prime = 2;
      n >>= 1;
    }
    for (int j = 3; j <= sqrt(n); j++) {
      while (n % j == 0) {
        max_prime = j;
        n /= j;
      }
    }
    if (n > 2)
      max_prime = n;

    // prime_check.pb(max_prime);
    get_high_prime[i] = max_prime;
  }
}

// const LL inf = 10e+9;
void max_self(LL &a, LL b) { a = max(a, b); }
const int maxi = 1e5 + 5;
vector<int> vect[100000];
vector<int> dp(maxi);
vi indegre(maxi);
LL j = 0;

vector<bool> visited(maxi);
void dfs(int k) {
  assert(!visited[k]);
  visited[k] = true;
  for (auto x : vect[k]) {
    dp[x] = max(dp[x], dp[k] + 1);
    --indegre[x];
    if (indegre[x] == 0)
      dfs(x);
  }
}
int main() {

  int n, e;
  cin >> n >> e;
  // visited.assign(n+1,false);
  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    vect[a].pb(b);
    ++indegre[b];
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i] && indegre[i] == 0)
      dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
#define endl "\n"
#define FILEIO                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define LL long long
#define mod 1000000007LL
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define pp pop_back
// #define p push
#define I insert
#define bg begin()
#define ed end()
#define sz size()
#define all(v) v.begin(), v.end()
typedef vector<LL> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef map<LL, LL> mii;
// typedef priority_queue<int> pq;
typedef queue<int> que;
typedef stack<int> stk;
#define Inf 100000005LL
#define f first
#define s second

vi vect_2pow;
void pow_of_2() {
  vect_2pow.assign(32, 0);
  for (int i = 0; i <= 32; i++) {
    LL y = 1 << i;
    vect_2pow[i] = y;
  }
}
bool Ispalindrome(string str) {
  LL len = str.size();
  bool flag;
  for (int i = 0; i < len / 2; i++) {
    if (str[i] == str[len - i - 1]) {
      flag = 0;
      continue;
    } else {
      flag = 1;
      break;
    }
  }
  if (flag == 1)
    return false;
  else
    return true;
}
vector<LL> prime_check;
mii get_high_prime;
int high_prime_factor() {
  // cout<<"ecd";
  LL max_prime = 0;
  for (int i = 2; i < 100000; i++) {
    LL n = i;

    while (n % 2 == 0) {
      max_prime = 2;
      n >>= 1;
    }
    for (int j = 3; j <= sqrt(n); j++) {
      while (n % j == 0) {
        max_prime = j;
        n /= j;
      }
    }
    if (n > 2)
      max_prime = n;

    // prime_check.pb(max_prime);
    get_high_prime[i] = max_prime;
  }
}

// const LL inf = 10e+9;
void max_self(LL &a, LL b) { a = max(a, b); }
const int maxi = 1e5 + 5;
vector<int> vect[maxi];
vector<int> dp(maxi);
vi indegre(maxi);
LL j = 0;

vector<bool> visited(maxi);
void dfs(int k) {
  assert(!visited[k]);
  visited[k] = true;
  for (auto x : vect[k]) {
    dp[x] = max(dp[x], dp[k] + 1);
    --indegre[x];
    if (indegre[x] == 0)
      dfs(x);
  }
}
int main() {

  int n, e;
  cin >> n >> e;
  // visited.assign(n+1,false);
  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    vect[a].pb(b);
    ++indegre[b];
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i] && indegre[i] == 0)
      dfs(i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}