Close
Topic list
Keys and Rooms
MEDIUM
30 mins
Arrays
Topics (Covered in this problem)
Problem solved
Skill meter
Arrays
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Keys and Rooms

Contributed by
Prateek_18cb
Medium
0/80
Avg time to solve 30 mins
Success Rate 70 %
Share

## Problem Statement

#### Note:

``````1. Room 0 is the only room that is initially unlocked and doesn’t require any key to enter.

2. Any other room can be visited only if you have the key to that room.

3. More than one room can have keys to the same room.

4. You are allowed to visit rooms in any order.

5. You can visit any room multiple times.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````1
5
2 1 2
1 0
2 1 0
1 4
1 3
``````
##### Sample Output 1:
``````False
``````
##### Explanation for sample input 1:
``````In the given example, room 0 has keys to room 1 and room 2. So we can go to room 1 and room 2 using these keys. But none of these two rooms has the keys to room 3 and room 4, so we can’t visit room 3 and room 4. Hence, the answer is False.
``````
##### Sample Input 2:
``````1
5
2 1 2
1 3
2 1 0
1 4
1 2
``````
##### Sample Output 2:
``````True
``````
##### Explanation for sample input 2:
``````In the given example, room 0 has keys to room 1 and room 2. Room 1 has the key to room 3, and room 3 has the key to room 4. So we can visit each and every room. Hence, the answer is True.
``````
Auto
Console