#include "leetcode.h" class Solution { public: bool canVisitAllRooms(vvd(int) & rooms) { stack dfs; dfs.push(0); unordered_set seen = {0}; while (!dfs.empty()) { int i = dfs.top(); dfs.pop(); for (int j : rooms[i]) { if (seen.count(j) == 0) { dfs.push(j); seen.insert(j); if (rooms.size() == seen.size()) return true; } } } return rooms.size() == seen.size(); } }; int main() { Solution obj; vvd(int) rooms = {{1}, {2}, {3}, {}}; if (obj.canVisitAllRooms(rooms)) cout << "true"; else cout << "false"; }