#C10486. Minimum Edges to Connect Graph
Minimum Edges to Connect Graph
Minimum Edges to Connect Graph
You are given an undirected graph with n nodes and m edges. Your task is to determine the minimum number of edges that need to be added to make the graph fully connected.
The graph is represented by nodes numbered from 1 to n and a list of m edges, each connecting a pair of nodes. Two nodes are in the same connected component if there is a path between them.
Hint: If there are c connected components, then the minimum number of edges required is c - 1 (i.e. connect every component together).
Mathematically, if we denote the number of connected components as \( c \), the answer is:
[ \text{Answer} = c - 1 ]
inputFormat
The first line of input contains a single integer T representing the number of test cases. The description of each test case follows.
For each test case, the first line contains two integers n and m, where n is the number of nodes and m is the number of edges. The next m lines each contain two integers u and v representing an edge between nodes u and v.
outputFormat
For each test case, output a single integer on a new line which is the minimum number of additional edges required to make the graph fully connected.
## sample3
5 3
1 2
2 3
4 5
4 0
3 3
1 2
2 3
1 3
1
3
0
</p>