Given a directed graph having A nodes.
A matrix B of size M x 2 is given which represents the edges such that there is a directed edge between
B[i][0] and B[i][1].
You have to find all the strongly connected subgraphs in the graph.
In a strongly connected graph, each pair of vertices if reachable from each other.
Return an array of array integers C which contains
all the nodes of strongly connected subgraphs and in a strongly connected subgraph all nodes need to be in sorted order while
you can return any order of strongly connected subgraphs.
For example:
if there are 6 nodes, (1, 2) belong to one scc, (5,3,6) belong to second scc and (4) belong to the third scc.
Then you can return any one of the following array of array integers:
[ [1, 2], [3, 5, 6], [4] ]
[ [1, 2], [4],[3, 5, 6] ]
[ [4], [3, 5, 6], [1, 2] ]
[ [4], [1, 2], [3, 5, 6] ]
[ [3, 5, 6], [1, 2], [4] ]
[ [3, 5, 6], [4], [1,2] ]
Note:
There are no self-loops in the graph.
The graph may or may not be connected.
Nodes are Numbered from 1 to A.
Your solution will run on multiple test cases. If you are using global variables make sure to clear them.
Input Format
The first argument given is an integer A.
The second argument given is the matrix B.
Output Format
Return the array of array integer C.
Constraints
1 <= A <= 100000
1<= B[i][0], B[i][1] <= A
For Example
Input 1:
A = 6
B = [ [5, 3]
[3, 5]
[1, 2]
[4, 1]
[2, 4]
[3, 4]
[5, 2]
[3, 1] ]
Output 1:
D = [ [3,5]
[1, 2, 4] ]
Input 2:
A = 5
B = [ [1, 2]
[2, 3]
[3, 4]
[4, 5] ]
Output 2:
D =[ [1 ]
[2 ]
[3 ]
[4 ]
[5 ] ]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.