You are given a network with âNâ system nodes [0 to N - 1] and âMâ connection. Your task is to find out all critical connections in a given network.
Note: A connection between node âuâ and âvâ is said to be a critical connection, if after removal of a connection âuâ - âvâ, there is no connection between node âuâ and âvâ and the network goes down.
For example:
For given N = 4, M = 4,

The connection between system node 0 and 1 is a critical connection.
The first line contains one positive integer âTâ, denoting the number of test cases, then âTâ test case follow
The first line of each test case contains two integers âNâ and âMâ, denoting the number of system nodes and the number of connections.
The next âMâ line contains two space-separated integers âuâ and âvâ, denoting the connection between âuâ and âvâ.
Output Format:
The first line of each test case contains one integer âXâ, denoting the number of critical connections.
The next âXâ lines contain two space-separated integers âuâ and âvâ, denoting critical connection between âuâ and âvâand âuâ is smaller than âvâ.
The output of each test case will be printed on 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 <= 5
1 <= N, M <= 10 ^ 6
0 <= u, v <= N - 1
There are no repeated connections.
Time Limit: 1 sec.
2
4 4
0 1
1 2
1 3
2 3
6 6
0 1
1 2
1 3
3 4
4 5
0 4
1
0 1
2
1 2
4 5
In the first test case, the given network looks like this:

The only connection between nodes 0 and 1 is critical. So, the number of critical connections is 1.
In the second test case, the given network looks like this:

The connection between nodes 1, 2, and 4, 5 is a critical connection. So, there are 2 critical connections. If anyone's failure makes the network go down
2
5 6
0 1
1 3
2 1
2 2
2 4
3 3
5 3
0 1
2 0
4 3
4
0 1
1 2
1 3
2 4
3
0 1
0 2
3 4
Can you take the help of dfs tree?
If we observe carefully the critical connection is a similar bridge in a graph(edge whose removal increases the number of components in a graph).
The idea is to do Depth first search(DFS) traversal of the given graph. In DFS tree an edge (âuâ, â vâ) (âuâ is parent of âvâ in DFS tree) is bridge if there does not exist any other alternative to reach âuâ or an ancestor of âuâ from subtree rooted with âvâ.
In order to implement the above thought, we will maintain two arrays :
The steps are as follows :
We will use following variables :
Let âcriticalConnection(n, âconnectionsâ)â be the function that returns all the critical connections in a network, where ânâ is the number of nodes in a given network and âconnectionsâ contain all connections.
This utility function finds all the critical connections in a network.
O(M + N), where âNâ is the number of nodes and âMâ is the number of connections.
We are using the standard âdfsâ function that takes O(N + M). Hence, the overall time complexity is O(N + M).
O(M + N), where âNâ is the number of nodes and âMâ is the number of connections.
Mainly, We are using space in creating a graph, which takes O(N + M) space, visited array, which takes O(N) space, âlowâ array which takes O(N) space, âdiscâ array which takes O(N) space. Hence, the overall space complexity is O(N + M) + 3 * O(N) = O(N + M).