You are given a directed graph of several connected components (maybe only one). Every node of the graph has at most one outgoing edge and at most one incoming edge. The graph does not contain any cycles or self-loops.
There is a weight associated with each edge of the graph. Your task is to find out the number of connected components, the starting and ending node of each component, and the minimum weight in that component. It is guaranteed that each component has at least one edge.
For Example :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.
Output format :
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.
Note :
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
2
6 3
1 2 5
3 4 8
6 5 10
9 6
1 2 5
2 3 1
3 4 8
5 6 10
6 8 9
7 9 15
3
1 2 5
3 4 8
6 5 10
3
1 4 1
5 8 9
7 9 15
For the first test case, the given graph is depicted in the below figure.

For the second test case, the given graph is depicted in the below figure.

2
2 1
2 1 8
6 5
1 2 7
2 3 9
3 4 10
4 5 12
5 6 2
1
2 1 8
1
1 6 2
Think of a DFS based approach.
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.
Steps:
dfs(src, &minWeight, start[], weights[]):
O(N + M), where N and M denote the number of nodes and the graph’s number of edges.
We are traversing each node of the graph, and the dfs function runs for each edge of the graph, so the overall time complexity is O(N + M).
O(N), where N denotes the number of nodes in the graph.
We are making three arrays start, end, weights of size N, and the res array of size C, where C is the number of components in the graph. Since C < N, so the space complexity is O(N).