


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.
The first line contains an integer 'T', which denotes the total number of test cases or queries to be run. Then, the 'T' test cases follow.
The first line of every test case contains an integer 'N', denoting the number of rooms. Then 'N' lines follow.
Each line consists of 'M+1' space-separated integers. The first integer denotes the total number of keys present in this room. The next M integers denote the numbers of the corresponding rooms whose keys are present in this room.
Note that the i-th line denotes information about the keys present in the i-th room.
For more clarity, please refer to the sample inputs.
For each test case, return “True” if it is possible to visit each and every room, otherwise return “False” if it’s not possible.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 5000
1 <= M <= 50
0 <= keys[i] < N
Time Limit: 1sec
The approach is to view this problem in the form of a graph and use the technique of Breadth-First Search. For this, we need a queue that will allow us to traverse the graph in a Breadth-First Search fashion. Then, we check whether there is only a single connected component in the graph or not. If the graph is a single connected component, that means all the rooms can be visited. So, the answer is “True” in that case. If it is not a single connected component, then the answer is “False”.
The approach is to view this problem in the form of a graph and use the technique of Depth First Search iteratively and keep a count of the visited 'ROOMS'. For this, we need a stack that will allow us to traverse the graph in a Depth First Search fashion. Then, we check whether the count of the visited 'ROOMS' is equal to the total number of 'ROOMS' or not. If the count is equal, that means all the 'ROOMS' can be visited. So, the answer is “True” in that case. If it is unequal, then the answer is “False”.