fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. vector<int> a[100005];
  5. ll dp[100005];
  6. ll dfs(ll u){
  7. if (dp[u] != - 1){
  8. return dp[u];
  9. }
  10. ll ans = 0;
  11. for (auto i : a[u]){
  12. ans = max(ans , dfs(i) + 1);
  13. }
  14. return dp[u] = ans;
  15. }
  16. void solve(){
  17. ll n , m;
  18. cin >> n >> m;
  19. for (int i = 1 ; i <= m ; i++){
  20. ll x ,y;
  21. cin >> x >> y;
  22. a[x].push_back(y);
  23. }
  24. memset(dp , -1 , sizeof(dp));
  25. ll ans = 0;
  26. for (ll i = 1 ; i <= n ; i++){
  27. ans = max(ans , dfs(i));
  28. }
  29. cout << ans;
  30. }
  31. int main(){
  32. solve();
  33. }
  34.  
Success #stdin #stdout 0.01s 6656KB
stdin
Standard input is empty
stdout
Standard output is empty