Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <functional>
#include <map>
#include <set>
#include <cmath>
using namespace std;

#define INT_MAX 2147483647
#define ll long long

ll max(ll a, ll b) { return (a < b) ? b : a; }


vector<int> lp;

void top_sort_util(int i, unordered_map<int, vector<int>>& edges, vector<bool>& visited, list<int>& stack) {
    visited[i] = true;

    for (int n : edges[i]) {
        if (!visited[n]) {
            top_sort_util(n, edges, visited, stack);
        }
    }
    stack.push_back(i);
}

list<int> top_sort(unordered_map<int, vector<int>>& edges, int n) {
    vector<bool> visited(n + 1);
    list<int> stack;
    for (int i = 1; i <= n; i++) {
        if (!visited[i]) {
            top_sort_util(i, edges, visited, stack);
        }
    }
    return stack;
}

void solve(unordered_map<int, vector<int>>& edges, int n) {
    list<int> stack = top_sort(edges, n);
    lp[stack.back()] = 0;

    for (auto it = stack.rbegin(); it != stack.rend(); it++) {
        int u = *it;

        if (lp[u] != -1) {
            for (int nei : edges[u]) {
                lp[nei] = max(lp[nei], lp[u] + 1);
            }
        }
    }

    int best = -1;
    for (int dist : lp) {
        best = max(dist, best);
    }

    cout << best << endl;
}

// DP !!
int main() {
    int n, m;
    cin >> n >> m;

    unordered_map<int,vector<int>> edges;
    lp.resize(n + 1, -1);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        edges[x].push_back(y);
    }

    solve(edges, n);
    
    return 0;
}

Compiler output:
a.cc:25:90: error: 'list' has not been declared
   25 | void top_sort_util(int i, unordered_map<int, vector<int>>& edges, vector<bool>& visited, list<int>& stack) {
      |                                                                                          ^~~~
a.cc:25:94: error: expected ',' or '...' before '<' token
   25 | void top_sort_util(int i, unordered_map<int, vector<int>>& edges, vector<bool>& visited, list<int>& stack) {
      |                                                                                              ^
a.cc: In function 'void top_sort_util(int, std::unordered_map<int, std::vector<int> >&, std::vector<bool>&, int)':
a.cc:30:46: error: 'stack' was not declared in this scope
   30 |             top_sort_util(n, edges, visited, stack);
      |                                              ^~~~~
a.cc:15:1: note: 'std::stack' is defined in header '<stack>'; this is probably fixable by adding '#include <stack>'
   14 | #include <cmath>
  +++ |+#include <stack>
   15 | using namespace std;
a.cc:33:5: error: 'stack' was not declared in this scope
   33 |     stack.push_back(i);
      |     ^~~~~
a.cc:33:5: note: 'std::stack' is defined in header '<stack>'; this is probably fixable by adding '#include <stack>'
a.cc: At global scope:
a.cc:36:1: error: 'list' does not name a type
   36 | list<int> top_sort(unordered_map<int, vector<int>>& edges, int n) {
      | ^~~~
a.cc: In function 'void solve(std::unordered_map<int, std::vector<int> >&, int)':
a.cc:48:5: error: 'list' was not declared in this scope
   48 |     list<int> stack = top_sort(edges, n);
      |     ^~~~
a.cc:15:1: note: 'std::list' is defined in header '<list>'; this is probably fixable by adding '#include <list>'
   14 | #include <cmath>
  +++ |+#include <list>
   15 | using namespace std;
a.cc:48:10: error: expected primary-expression before 'int'
   48 |     list<int> stack = top_sort(edges, n);
      |          ^~~
a.cc:49:8: error: 'stack' was not declared in this scope
   49 |     lp[stack.back()] = 0;
      |        ^~~~~
a.cc:49:8: note: 'std::stack' is defined in header '<stack>'; this is probably fixable by adding '#include <stack>'