Thomas is a Tank engine on the island of Sodor. There are total ‘N’ cities on this island connected with ‘m’ tracks. All the cities on this island are connected to each other, directly or indirectly. Thomas and all his engine friends travel from city ‘a’ to city ‘b’ daily but might use different routes. Some of these tracks are present on all the paths from the city ‘a’ to ‘b’. Due to excessive load on these tracks, all of these tracks are not in good condition, so one day Thomas asks The Fat Controller to repair all the tracks that are present on all the paths from ‘a’ to ‘b’. Help the Fat controller to count all these tracks.
For Example:n = 5, m = 5, a = 1, b = 0
Now in this example, all the paths from ‘1’ to ‘0’ are:
1 -> 4 -> 0
1 -> 5 -> 4 -> 0
The track 4 -> 0 is present in both of these paths. Hence the answer is 1.
The first line contains a single integer ‘T’ denoting the number of test cases. Then each test case follows:
The first line of each test case contains two integers, ‘N’ and ‘M’, denoting the number of cities and tracks, respectively.
The next ‘M’ lines of the test case contain two space-separated integers, ‘x’ and ‘y’, denoting a track between ‘x’ and ‘y’.
The last line of the test case contains two space-separated integers, ‘a’ and ‘b’, denoting the starting and ending city of Thomas and his friends.
Output Format :
For each test case, print the total number of common tracks between all the paths from ‘a’ to ‘b’.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
1 <= T <= 10
1 <= N <= 1000
N-1 <= M <= (V * (V - 1)) / 2
0 <= a, b <= V - 1
Time Limit: 1sec
2
4 4
0 1
0 3
1 2
2 3
1 3
6 6
1 2
2 3
2 4
4 5
3 5
5 0
1 0
0
2
In the first test case, there are two paths from 1 to 3. I.e. 1 -> 2 -> 3 or 1 -> 0 -> 3 and both these paths have no common track.
Hence the answer is 0.
In the second test case, there are two different paths from 1 to 0. i.e. , 1 -> 2 -> 3 -> 6 -> 0 and 1 -> 2 -> 4 -> 6 -> 0. Having two common tracks, 1 -> 2 and 6 -> 0.
Hence the answer is 2.
2
2 1
0 1
1 0
5 4
0 1
1 2
2 3
3 4
0 4
1
4
Iterate through the graph using DFS and count all the common paths using the concept of bridges.
In this approach, iterate through the whole graph using DFS and take three vectors inTime, low, outTime, in which we will store the first time we will visit that node, the minimum time we encountered the node and the time when we finally left the current node, respectively.
The steps are as follows :
O(V + E). Where V is the number of cities and E is the number of Tracks.
Since we are implementing Depth-first search, which is a linear task,
Hence the time complexity is O(V + E).
O(V), Where V is the number of cities.
We are using arrays of size V.
Hence the space complexity is O(V).