1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: bool canVisitAllRooms(vector<vector<int>>& rooms) { queue<int> keys; keys.push(0); unordered_set<int> ok = {0}; while(!keys.empty()){ int rn = keys.front(); keys.pop(); ok.emplace(rn); for(int key : rooms[rn]){ if(ok.find(key) == ok.end()) keys.push(key); } } return ok.size() == rooms.size(); } };
|