


You have been given a graph consisting of ‘N’ vertices numbered from 1 to ‘N’. The graph has ‘M’ edges. In an operation, you can shift an edge between two directly connected vertices to between pair of disconnected vertices to make them directly connected. Return the minimum number of operations to make the graph connected. If it is not possible to make graph connected return -1.
Example:Let’s say ‘N’ is 4 and ‘M' is 3. The 3 edges are (1,2), (2,3) and (1,3). Then our graph will look as follows:-

To make the graph connected we can shift the edge between (1,3) to (1,4). This operation will make the graph connected. There are multiple ways in which we can make graph connected. So, in this case, we can make graph connected in just one operation.
Note:
1. A connected graph is a graph that is connected in the sense of a topological space, i.e., there is a path from any vertex to any other vertex in the graph.
2. There are no repeated edges and self-loops in the graph.
3. You do not need to print anything; it has already been taken care of. Just implement the function.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ representing the number of vertices and the number of edges in the graph.
Each of the next ‘M’ lines contains two space-separated integers representing the vertices that are directly connected by an edge.
Output Format:
For each test case, print a single line containing a single integer denoting the minimum number of operations to make the graph connected. If it is not possible to make graph connected return -1.
The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10000
1 <= M <= 10000
1 <= U[i], V[i] <= N
Where ‘T’ is the number of test cases.‘N’ is the number of vertices in the graph. ‘M’ is the number of edges in the graph. ‘U[i]’ and ‘V[i]’ are vertices of the i-th edge.
Time Limit: 1sec.
2
4 3
1 2
2 3
3 1
4 3
1 2
2 3
3 4
1
0
Test case 1:
The graph looks as follows:-

Initially, graph is disconnected. We can apply one operation and shift the edge between (2,3) to (3,4). This operation will make the graph connected. The graph after the operation will look as follows:-

Therefore the answer is 1.
Test case 2:
The graph looks as follows:-

The graph is already connected so we don’t need to make any operation.
Therefore the answer is 0.
2
4 2
1 2
3 4
4 6
1 2
1 3
1 4
2 3
2 4
3 4
-1
0
Find the number of connected components.
For a graph with ‘N’ vertices to be connected, there must be at least ‘N’ - 1 edges in the graph. If a graph has less than ‘N' - 1 edges it is impossible to make the graph connected. Otherwise, it is always possible to make graph connected. As we need to find the minimum number of operations to make the graph connected we will think greedily. We will find the total number of connected components in the graph. We can treat each connected component in the graph as a single vertex. Basically, we want to shift edges such that these components become connected. Therefore answer will be the total number of connected components - 1.
We will apply the algorithm as follows:-
O(N + M), where N denotes the number of vertices and M denotes the number of edges in the graph.
We are visiting each vertex and edge once.
O(N + M), where N denotes the number of vertices and N denotes the number of edges in the graph.
We build an adjacency list where ‘i-th’ vector/list can be of size N.