fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> adj[100111];
  6. int res;
  7. bool visited[100111];
  8. int dp[100111];
  9. int m, n;
  10.  
  11. int DFS(int u) {
  12. if (dp[u]) return dp[u];
  13.  
  14. for (int i = 0; i < adj[u].size(); i += 1) {
  15. int v = adj[u][i];
  16. dp[u] = max(dp[u], DFS(v) + 1);
  17. }
  18.  
  19. return dp[u];
  20. }
  21.  
  22. int32_t main() {
  23. ios_base::sync_with_stdio(false);
  24. cin.tie(NULL);
  25.  
  26. cin >> m >> n;
  27. for (int i = 1, u, v; i <= n; i += 1) {
  28. cin >> u >> v;
  29. adj[u].push_back(v);
  30. }
  31.  
  32. for (int i = 1; i <= m; i += 1) {
  33. res = max(res, DFS(i));
  34. }
  35.  
  36. cout << res;
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 6080KB
stdin
Standard input is empty
stdout
Standard output is empty