
The example of a graph is shown in the below figure.

So, there are two connected components in the above graph. The starting and ending node of one component is 1 and 3, respectively, and the minimum weight in this component is 9. The starting and ending node in the other component is 4 and 5 respectively, and the minimum weight is 11.
The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains two space-separated integers N and M, denoting the number of nodes and the number of edges in the graph, respectively.
Then M lines follow. Each line contains three space-separated integers denoting the source, destination, and weight of each edge.
For each test case, print the number of connected components. Let’s denote this as C. Then C lines follow.
Each line contains three space-separated integers representing each component’s starting and ending node and the minimum weight in that component.
Output for each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N, M <= 5000
1 <= Source and Destination of an edge <= N
1 <= weight of an edge <= 10^5
Time limit: 1 second
The idea here is to traverse each node of the graph. If a node doesn’t have an incoming edge but has an outgoing edge, then we should start a Depth First Search from this node passing another variable by reference to store the minimum weight in this component. We return the last node of the component from this function.
The idea is exactly similar to the previous approach, but here we will use a Breadth-First Search instead of a Depth-First Search.