0841.cpp (504B)
1 class Solution { 2 public: 3 bool canVisitAllRooms(vector<vector<int>> &rooms) { 4 unordered_set<int> us; 5 queue<int> q; 6 7 q.push(0); 8 us.insert(0); 9 while (!q.empty()) { 10 int room = q.front(); 11 q.pop(); 12 for (int key : rooms[room]) { 13 if (!us.count(key)) { 14 us.insert(key); 15 q.push(key); 16 } 17 } 18 } 19 return us.size() == rooms.size(); 20 } 21 };