Advertisement
sidjha57

Longest Path

Jul 19th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. //https://atcoder.jp/contests/dp/tasks/dp_g
  2.  
  3.  
  4. #include<bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. #define SET(a)                   memset( a, -1, sizeof a )
  9. #define loop(i,a,b)              for(int i=(a);i<=(b);i++)
  10. #define _fast                    ios_base::sync_with_stdio(0);  cin.tie(0);
  11.  
  12.  
  13. vector <int> graph[100005];
  14. int dp[100005];
  15. int FindlongestPath (int& src) {
  16.     if (dp[src] != -1) return dp[src];
  17.     int result = INT_MIN;
  18.     bool noNeighbors = true;
  19.     for (auto neighbors: graph[src]) {
  20.         noNeighbors = false;
  21.         result = max(result, FindlongestPath(neighbors));
  22.     }
  23.  
  24.     return (noNeighbors) ? 0: result + 1;
  25. }
  26. void solve() {
  27.     int n,m; cin >>n >>m;
  28.     loop(i,1,m) {
  29.         int a,b; cin >>a >>b;
  30.         graph[a].emplace_back(b);
  31.     }
  32.     SET(dp);
  33.     int ans = INT_MIN;
  34.     loop (src,1,n) {
  35.         ans = max(ans,FindlongestPath(src));
  36.     }
  37.     cout << ans;
  38. }
  39.  
  40. int main(int argc, char const *argv[]){
  41.     _fast
  42.     int t=1;
  43.     while(t--){
  44.      solve();
  45.     }
  46.   return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement