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).