Advertisement
vaibhav1906

keys and rooms | sonu

Aug 24th, 2022
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. class Solution {
  2. public:
  3.    
  4.     void dfs(vector<vector<int>>&rooms, int node,vector<bool>&v, int &count){
  5.        
  6.         if(v[node])return;
  7.         v[node] = true;
  8.         count++;
  9.        
  10.         for(int i = 0; i<rooms[node].size(); i++){
  11.             int newNode = rooms[node][i];
  12.             dfs(rooms,newNode, v,count);
  13.         }
  14.        
  15.     }
  16.    
  17.     bool canVisitAllRooms(vector<vector<int>>& rooms) {
  18.       int n = rooms.size();
  19.         vector<bool>v(n,false);
  20.         int count = 0;
  21.         dfs(rooms, 0, v,count);
  22.        
  23.         return count==n;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement