Problem p03166 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(                                                          \
    "Ofast") // Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout << fixed;                                                               \
  cout << setprecision(10);
#define randomINIT                                                             \
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\n"
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\n";
#define deb1(x) cerr << #x << " = " << x << '\n';
#define deb2(x, y)                                                             \
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \
       << '\n';
#define deb3(x, y, z)                                                          \
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \
       << y << ',' << z << ']' << '\n';

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const ll MOD = 1e+9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MAXN = 1e+5 + 7;

vll adj[MAXN];
ll visited[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, m, x, y;
stack<ll> Stack;
vll v1;
ll lis[100005];
//<<-----Implement Functions Here---->>//
void topologicalSortUtil(ll v) {
  visited[v] = 1;
  for (auto it : adj[v])
    if (!visited[it])
      topologicalSortUtil(it);
  Stack.push(v);
}

//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  for (ll i = 1; i <= n; i++)
    lis[i] = 1;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      topologicalSortUtil(i);
  }
  while (!Stack.empty()) {
    v1.pb(Stack.top());
    Stack.pop();
  }
  // for(auto it:v1)cout<<it<<" ";cout<<endl;
  for (auto it : v1) {
    for (auto it1 : adj[it]) {
      lis[it1] = max(lis[it1], lis[it] + 1);
    }
  }
  ll max1 = -1;
  for (ll i = 1; i <= n; i++)
    max1 = max(lis[i], max1);
  cout << max1 - 1;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }
}

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout << fixed;                                                               \
  cout << setprecision(10);
#define randomINIT                                                             \
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define frr(i, a, n) for (ll i = (a); i >= n; i--)
#define fo(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\n"
#define pb push_back
#define mp make_pair
#define s second
#define f first
#define dline cerr << "///REACHED///\n";
#define deb1(x) cerr << #x << " = " << x << '\n';
#define deb2(x, y)                                                             \
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \
       << '\n';
#define deb3(x, y, z)                                                          \
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \
       << y << ',' << z << ']' << '\n';

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const ll MOD = 1e+9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MAXN = 1e+5 + 7;

vll adj[MAXN];
ll visited[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

//<<-----Declare Variable Here------->>//
int t = 1;
ll n, m, x, y;
stack<ll> Stack;
vll v1;
ll lis[100005];
//<<-----Implement Functions Here---->>//
void topologicalSortUtil(ll v) {
  visited[v] = 1;
  for (auto it : adj[v])
    if (!visited[it])
      topologicalSortUtil(it);
  Stack.push(v);
}

//<<-----Start of Main--------------->>//
void MAIN() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> x >> y;
    adj[x].pb(y);
  }
  for (ll i = 1; i <= n; i++)
    lis[i] = 1;
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      topologicalSortUtil(i);
  }
  while (!Stack.empty()) {
    v1.pb(Stack.top());
    Stack.pop();
  }
  // for(auto it:v1)cout<<it<<" ";cout<<endl;
  for (auto it : v1) {
    for (auto it1 : adj[it]) {
      lis[it1] = max(lis[it1], lis[it] + 1);
    }
  }
  ll max1 = -1;
  for (ll i = 1; i <= n; i++)
    max1 = max(lis[i], max1);
  cout << max1 - 1;
}

int main() {
  fastio;
  randomINIT;

  // cin>>t;
  while (t--) {
    MAIN();
  }
}