Description

There are n rooms. Room 0 is open, others are locked. Each room contains keys to other rooms. Return true if you can visit all rooms.

Examples

Input:rooms = [[1],[2],[3],[]]
Output:true
Explanation:

Can visit all rooms.

Input:rooms = [[1,3],[3,0,1],[2],[0]]
Output:false
Explanation:

For rooms = [[1,3],[3,0,1],[2],[0]], the answer is false because the keys and rooms condition is not met.

Input:rooms = [[1,2],[],[0,3],[2]]
Output:true
Explanation:

Starting from room 0, this gives keys [1,2]. From room 1 (empty), no new keys. From room 2, this gives keys [0,3]. Now it is possible to visit room 3, which gives us key [2]. All rooms 0,1,2,3 can be visited.

Constraints

  • 1 ≤ n ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!