Problem p03166 (C++)

Buggy submission (status: Runtime Error):
// Take care of mod becoming -ve

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define fro(i, s, e) for (auto i = s; i <= e; ++i)
#define int long long
#define ld long double
#define fr(i, n) for (auto i = 0; i < n; i++)
#define ff first
#define pf pop_front()
#define ss second
#define mp make_pair
#define vi std::vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int, int>
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FILE                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define inp(a)                                                                 \
  int a;                                                                       \
  cin >> a;
#define ps(x, y) fixed << setprecision(y) << x
#define mkarr(arr, n, type) type *arr = new type[n];
#define endl "\n"
#define sz size()
#define rev(i, e, s) for (auto i = e; i >= s; --i)
const int mod = 1000000007;
#define min_pq priority_queue<int, std::vector<int>, greater<int>>
#define printclock                                                             \
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
using namespace std;
const int N = 100005;
const int inf = 1000000000000000000;
#define fit(s, x) memset(s, x, sizeof(s));
#define out(x) cout << x << "\n"
#define deb(x) cout << #x << x << "\n"
#define pi 3.14159265358979323846264338327950
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by
// find(all(x),key)-x.begin() find_if(iterator_first,iterator_last,unary
// function) returns first iterator if any element corresponding to unary
// function exists and if no such element is found it retrurns the last iterator
/*For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; *it returns 25*/
/*search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1
based on pred function the iterator correspond to the first element of sequence
if it exists and if not then th elast iterator is obtained.*/
// use substr(size_t pos,size_t len) for the substrings
//  iota(forward itr,backwrd itr,val) can be used for consecutive numbers
int binsearch(int *a, int start, int end, int key) {
  while (start <= end) {
    int mid = start + (end - start) / 2;
    if (a[mid] < key)
      start = mid + 1;
    else if (a[mid] > key)
      end = mid - 1;
    else
      return mid;
  }
  return -1;
}
int power(int a, int b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1LL) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b = (b >> 1LL);
  }
  return ans;
}

vector<int> gr[N];
int dis[N];
int in_deg[N];
bool visited[N];
void dfs(int a) {
  assert(!visited[a]);
  visited[a] = true;
  for (int b : gr[a]) {
    dis[b] = max(dis[b], dis[a] + 1);
    --in_deg[b];
    if (in_deg[b] == 0) {
      dfs(b);
    }
  }
}
int32_t main() {
  fio;
  // indexed_set s;
  // s.insert(2);
  // auto y = s.find_by_order(7);
  // cout << *x << " " << *y <<
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    gr[a].pb(b);
    ++in_deg[b];
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dis[i]);
  }
  out(ans);
  printclock;
  return 0;
}


Fixed submission:
// Take care of mod becoming -ve

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define fro(i, s, e) for (auto i = s; i <= e; ++i)
#define int long long
#define ld long double
#define fr(i, n) for (auto i = 0; i < n; i++)
#define ff first
#define pf pop_front()
#define ss second
#define mp make_pair
#define vi std::vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int, int>
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FILE                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define inp(a)                                                                 \
  int a;                                                                       \
  cin >> a;
#define ps(x, y) fixed << setprecision(y) << x
#define mkarr(arr, n, type) type *arr = new type[n];
#define endl "\n"
#define sz size()
#define rev(i, e, s) for (auto i = e; i >= s; --i)
const int mod = 1000000007;
#define min_pq priority_queue<int, std::vector<int>, greater<int>>
#define printclock                                                             \
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
using namespace std;
const int N = 100005;
const int inf = 1000000000000000000;
#define fit(s, x) memset(s, x, sizeof(s));
#define out(x) cout << x << "\n"
#define deb(x) cout << #x << x << "\n"
#define pi 3.14159265358979323846264338327950
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
// Use __gcd(a,b) for gcd
// find(all(x),key) to get the iterator and its index is obtained by
// find(all(x),key)-x.begin() find_if(iterator_first,iterator_last,unary
// function) returns first iterator if any element corresponding to unary
// function exists and if no such element is found it retrurns the last iterator
/*For example:
find_if(all(x),isodd) for x being a vector{10,25,40,55}; *it returns 25*/
/*search(all(v1),all(v2),pred) searches for all the elements of v2 present in v1
based on pred function the iterator correspond to the first element of sequence
if it exists and if not then th elast iterator is obtained.*/
// use substr(size_t pos,size_t len) for the substrings
//  iota(forward itr,backwrd itr,val) can be used for consecutive numbers
int binsearch(int *a, int start, int end, int key) {
  while (start <= end) {
    int mid = start + (end - start) / 2;
    if (a[mid] < key)
      start = mid + 1;
    else if (a[mid] > key)
      end = mid - 1;
    else
      return mid;
  }
  return -1;
}
int power(int a, int b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1LL) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b = (b >> 1LL);
  }
  return ans;
}

vector<int> gr[N];
int dis[N];
int in_deg[N];
bool visited[N];
void dfs(int a) {
  assert(!visited[a]);
  visited[a] = true;
  for (int b : gr[a]) {
    dis[b] = max(dis[b], dis[a] + 1);
    --in_deg[b];
    if (in_deg[b] == 0) {
      dfs(b);
    }
  }
}
int32_t main() {
  fio;
  // indexed_set s;
  // s.insert(2);
  // auto y = s.find_by_order(7);
  // cout << *x << " " << *y <<
  int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b;
    cin >> a >> b;
    gr[a].pb(b);
    ++in_deg[b];
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i] && in_deg[i] == 0) {
      dfs(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dis[i]);
  }
  out(ans);
  printclock;
  return 0;
}