Advertisement
jibha

Untitled

Feb 4th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4. int dfs(int i,map<int,map<int,int>>& e,map<int,int>& visited,vector<bool>& hasApple){
  5.  
  6. if(visited[i]==1){
  7. return 0;
  8. }
  9. visited[i]=1;
  10.  
  11. int val=0;
  12.  
  13. for(auto iter:e[i]){
  14. if(hasApple[iter.first]==1&&visited[iter.first]==0){
  15. cout<<i<<":"<<iter.first<<" ";
  16. val+=2;
  17. }
  18. int dfsval=dfs(iter.first,e,visited,hasApple);
  19. if(dfsval!=0){
  20. val+=2*(1-visited[iter.first])+dfsval;
  21. }
  22. }
  23.  
  24. return val;
  25.  
  26.  
  27. }
  28.  
  29.  
  30. int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
  31.  
  32. map<int,map<int,int>> e;
  33.  
  34. for(auto iter:edges){
  35. e[iter[1]][iter[0]]=1;
  36. e[iter[0]][iter[1]]=1;
  37. }
  38.  
  39. map<int,int> visited;
  40.  
  41. return dfs(0,e,visited,hasApple);
  42.  
  43.  
  44. }
  45. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement