


The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'M', denoting the number of computers and the number of wires, respectively.
The next 'M' lines of each test case contain the description of the 'M' wires
The 'i'th' line contains two space-separated integers, 'Computer1', and 'Computer2' . 'Computer1'' and 'Computer2' denote the two computers that are connected by the 'i'th' wire.
The only line of output of each test case should contain the minimum number of wires that should be removed and rearranged so that it is possible to transmit information between all pairs of computers.
Print the output of each test case in a new line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= M <= min(10^5, (N*(N-1))/2)
1 <= Computer1, Computer2 <= N
Any two computers are directly connected by at most one wire.
Time Limit: 1 sec
Let us first find the condition for which it is impossible to replace wires to connect all computers. The only necessary and sufficient condition to check is that the number of wires should always be greater than or equal to the number of computers - 1, i.e., M > = ( N - 1 ). If this condition holds true, then the answer can never be -1. Otherwise, the answer will always be -1. This condition is necessary because to form a connected network of computers using minimum wires, we need to form a tree-like structure, and the number of edges in a tree having N nodes is N - 1.
Now we will remodel the above problem as a graph problem. The computers will act as the graph vertices, and the wires will act as the graph edges. The problem now reduces to find the number of connected components in the graph. If the number of connected components in the graph is K, then our answer will be K - 1. This works because here, a connected component represents a network of computers that can transmit information among them. We already have checked that there are sufficient wires available, so we can take a wire from a connected component having an extra wire and use it to connect to another component to make a bigger connected component. We will need to do this step until all the connected components are not joined together. If we simulate the above process for some small graphs. We can observe that our answer will always be numberOfConnectedComponents - 1.
We can find the connected components and the number of connected components in a graph using Depth-First search (DFS). Reference.
Algorithm
The basic idea of this approach is to use a Disjoint Set Union data structure to find the number of connected components in the graph. We have seen in approach 1 that the problem is finally reduced to finding the number of connected components in an undirected graph. So we can use DSU to do this task very efficiently. We will use the union by size for optimisation.
Algorithm