fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<vector<int> > graph(10,vector<int >());
  5. map<int,int> m;
  6.  
  7. int bfs(int k)
  8. {
  9. queue<pair<int,int> > q;
  10. q.push({k,0});
  11. int maxP = INT_MIN;
  12. while(q.size() != 0){
  13. int val = q.front().first;
  14. int pos = q.front().second;
  15. maxP = max(maxP,pos);
  16. // cout<<val<<" ";
  17. q.pop();
  18. for(int i = 0; i < graph[val].size() ; i++){
  19. q.push({graph[val][i],pos+1});
  20. }
  21. }
  22. return maxP;
  23. }
  24. int main() {
  25.  
  26. graph[0].push_back(3);
  27. graph[0].push_back(2);
  28. graph[3].push_back(4);
  29. graph[4].push_back(5);
  30.  
  31. cout<<bfs(0)<<endl;
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 3460KB
stdin
1
2
10
42
11
stdout
3