Posted on

Description

Submission

class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        int n = rooms.size();
        vector<int> visited(n, 0);

        queue<int> q;
        q.push(0);

        while(!q.empty()) {
            int node = q.front();
            q.pop();
            
            if(visited[node]) continue;
            visited[node] = 1;

            for(auto nextNode: rooms[node]) {
                if(visited[nextNode]) continue;
                q.push(nextNode);
            }
        }

        for(auto x: visited) {
            if(!x) return false;
        }

        return true;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *