#C942. Network Connectivity Checker
Network Connectivity Checker
Network Connectivity Checker
You are given a network of devices, each with a list of neighbors. Each device is identified by a unique integer from 1 to N. For each test case, your task is to determine whether the network is connected. A network is considered connected if starting from device 1, you can reach every other device via the given neighbor connections.
The connectivity check can be formulated using the following idea. Let \(G = (V,E)\) be a directed graph where each device in \(V\) has directed edges to all of its neighbors in \(E\). The graph is connected if and only if a breadth-first search (BFS) starting from the node with ID 1 eventually visits all \(N\) nodes.
Note: Each device provides a list where the first integer is the device number and the remaining integers (if any) specify its direct neighbors. In some cases, a device might have no outgoing connection (i.e. the list contains only the device id), making it impossible to traverse from other nodes.
inputFormat
The input begins with an integer \(T\), representing the number of test cases. Each test case is described as follows:
- The first line contains an integer \(N\), the number of devices.
- Then, \(N\) lines follow. Each of these lines contains a sequence of space-separated integers. The first integer is the device id, and the subsequent integers (if any) represent the devices it can directly connect to.
All input is provided via standard input (stdin).
outputFormat
For each test case, output a single line with either Connected
if the network is connected, or Not Connected
if it is not. The output for all test cases should be printed to standard output (stdout), each result on a new line.
2
4
1 2 3
2 1 4
3 1
4 2
3
1 2
2 1 3
3 2
Connected
Connected
</p>