

Let's consider 'S' = 0 and 'D' = 4. In the above graph, the path from nodes 0 to 4 can be created by only reversing the edge (0, 5). Hence, the answer is 1.
The first line of input contains an 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 nodes and edges in the graph.
The following M lines of the test case contain two space-separated integers, ‘A’ and ‘B’, representing an edge from node ‘A’ to node ‘B’.
The last line of the test case contains two space-separated integers, ‘S’ and ‘D’, denoting the start and end node.
For each test case, print a single integer, i.e., the minimum number of edges to be reversed, so the path exists from ‘S’ to ‘D’.
Print the output of each test case 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 <= 5
2 <= N <= 10^5
N - 1 <= M <= 10^5
0 <= A, B < N
0 <= S, D < N
Time Limit: 1 sec
In this approach, we make the graph into a weighted graph. We set the edges’ edge-weight as 0 and add all the reverse edges with weight 1.
We will apply the Bellman-Ford algorithm on the start node and get the distance from all the nodes in the graph. Each time a reversed edge is traversed in the path, the path cost will increase by 1.The total path cost gives the number of reversed edges used in getting the path from source to destination.
We will create a function bellmanFord(edges, n, source). The parameter edge is the edge list, n is the number of nodes, and the source is the starting node. This function will return the list of distances of all nodes from the source.
Algorithm:
In this approach, we make the graph into a weighted graph. We set the edges’ edge-weight as 0 and add all the reverse edges with weight 1.
We will apply Dijkstra’s algorithm on the start node and get the distance from all the nodes in the graph. Each time a reversed edge is traversed in the path, the path cost will increase by 1. The total path cost gives the number of reversed edges used in getting from source to destination.
We will create a function dijkstra(graph, n, source). The parameter graph is the adjacency list, n is the number of nodes, and the source is the starting node. This function will return the list of distances of all nodes from the source.
Algorithm: