Advertisement
nikunjsoni

841

Apr 24th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     unordered_map<int, bool> seen;
  4.     bool canVisitAllRooms(vector<vector<int>>& rooms) {
  5.         stack<int> stk;
  6.         stk.push(0);
  7.         seen[0] = true;
  8.        
  9.         while(!stk.empty()){
  10.             int curr = stk.top();
  11.             stk.pop();
  12.             for(int next: rooms[curr]){
  13.                 if(!seen.count(next)){
  14.                     seen[next] = true;
  15.                     stk.push(next);
  16.                 }
  17.             }
  18.         }
  19.         return seen.size() == rooms.size();
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement