---
# System prepended metadata

title: 841. Keys and Rooms
tags: [graph]

---

---
title: 841. Keys and Rooms
tags: graph
description: share source code.
---

# 841. Keys and Rooms

```java=
class Solution {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        Queue<Integer> q = new LinkedList<>();
        q.offer(0);
        
        Set<Integer> visited = new HashSet<>();
        
        while(!q.isEmpty()){
            int key = q.poll();
            
            if(visited.contains(key)){
                continue;
            }
            
            visited.add(key);
            
            for(int m : rooms.get(key)){
                q.offer(m);
            }
        }
        
        return visited.size() == rooms.size();
    }
}
```

