#C11428. Graph Connectivity Check

    ID: 40743 Type: Default 1000ms 256MiB

Graph Connectivity Check

Graph Connectivity Check

You are given a series of road maps represented as undirected graphs. Each graph consists of intersections and roads connecting them. Your task is to determine whether the graph is connected; that is, whether every intersection can reach every other intersection directly or indirectly.

Formally, given a graph \(G=(V,E)\), it is connected if and only if $$ \forall u,v \in V, \; \exists\; \text{a path from } u \text{ to } v. $$ If there are no intersections (i.e., \(N=0\)), the graph is considered disconnected.

inputFormat

The input is given via standard input. The first line contains a single integer \(T\) representing the number of test cases. Each test case is described as follows:

  • The first line contains two integers \(N\) and \(E\), where \(N\) is the number of intersections and \(E\) is the number of roads.
  • The next \(E\) lines each contain two integers \(u\) and \(v\) indicating that there is a bidirectional road between intersection \(u\) and intersection \(v\).

outputFormat

For each test case, output a single line containing either YES if the graph is connected, or NO otherwise, via standard output.

## sample
6
4 3
1 2
2 3
3 4
4 2
1 2
3 4
3 2
1 2
2 3
3 1
1 2
1 0
0 0
YES

NO YES NO YES NO

</p>