Advertisement
jayati

Keys and Rooms

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