Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool canVisitAllRooms(vector<vector<int>>& rooms) {
- int n = (int)rooms.size();
- if(!n) return 1;
- vector<int>visit(n,0);
- dfs(0, rooms, visit);
- bool ans = true;
- for( int i: visit)
- {
- ans&=i==1;
- }
- return ans;
- }
- private:
- void dfs(int node, vector<vector<int>>&rooms, vector<int>&visit)
- {
- visit[node] = 1;
- for( int neighbour : rooms[node])
- {
- if( !visit[neighbour] )
- dfs(neighbour, rooms, visit);
- }
- }
- };
Add Comment
Please, Sign In to add comment