Connected or Not Connected
Time limit1sMemory limit128 MB
Given a graph with n sites and k edges, decide whether every site can reach every other.
- Level
Easy3 of 10
- Topics
- Graph, DFS, Union-find
- Solved
- No attempts yet
Problem
There is a world containing sites that are interconnected by a number of (virtual) pathways. Each pathway is a bi-directional connection between two sites: if there is a connection between site and site , then you can travel from to and from to .
Using only the given connections, decide whether there is a path from every site to every other site — that is, whether the graph is connected as a single whole.
Input
The first line contains the number of test cases ().
Each of the following lines contains one test case. The first number in a test case is the number of sites (); the sites are numbered from to . The second number is the number of connections (). After that come connections, each given as a pair of integers.
Connections may repeat, and there may be self-connections (a connection from a site to itself).
Output
For each test case, check whether there is a path from every site to every other site, and print Connected. if so, or Not connected. otherwise, on its own line.