#K5736. Taco: Fully Connected Graph
Taco: Fully Connected Graph
Taco: Fully Connected Graph
You are given an undirected graph represented as \( G = (V, E) \), where \( V \) is a set of \( n \) vertices and \( E \) is a set of \( m \) edges. The task is to determine if the graph is fully connected — that is, whether all vertices belong to a single connected component.
A graph is fully connected if for every pair of vertices there exists a path connecting them. You may use algorithms such as breadth-first search (BFS) or union-find to solve the problem. In mathematical terms, the graph is fully connected if and only if the number of connected components is 1.
For each test case, the input starts with two integers \( n \) and \( m \) followed by \( m \) lines each containing two integers \( u \) and \( v \), indicating an undirected edge between vertices \( u \) and \( v \). Nodes are labeled from 1 to \( n \).
Output "YES" if the graph is fully connected, otherwise output "NO".
inputFormat
The first line contains an integer T, the number of test cases. For each test case, the first line contains two integers n and m, denoting the number of vertices and edges respectively. Each of the next m lines contains two integers u and v representing an undirected edge between nodes u and v. Nodes are labeled from 1 to n.
outputFormat
For each test case, output a single line containing "YES" if the graph is fully connected, otherwise "NO".## sample
3
4 2
1 2
2 3
5 4
1 2
2 3
3 4
4 5
3 0
NO
YES
NO
</p>