class Solution:
    def canVisitAllRooms(self, rooms):
        visited = [False]*len(rooms)
        visited[0] = True
        r = rooms[0]
        while len(r) > 0:
            nr = []
            for x in r:
                if not visited[x]:
                    nr += rooms[x]
                    visited[x] = True
            r = nr
        for x in visited: 
            if not x: return False
        return True

print(Solution().canVisitAllRooms([[1],[2],[3],[]]))
print(Solution().canVisitAllRooms([[1,3],[3,0,1],[2],[0]]))
