Leaderboard
Showing:
Handle Score
@Ibrahim Nash 5725
@blackshadows 5685
@akhayrutdinov 5111
@mb1973 4989
@Quandray 4908
@saiujwal13083 4156
@sanjay05 3762
@marius_valentin_dragoi 3511
@sushant_a 3453
@KshamaGupta 3318
@the_coder95 3302
Complete Leaderboard
Strongly connected component (Tarjans's Algo)
Hard Accuracy: 33.8% Submissions: 1847 Points: 8

Given an unweighted directed graph, your task is to print the members of the strongly connected component in the graph where each component is separated by ', ' (see the example for more clarity). The Graph can have loops.

Input:
The first line of the input consists of 'T' denoting the number of test cases. Each test case consists of two lines, the first line of each test case consist of two integers N and M, denoting the number of vertices and edges respectively. Then 'M' lines follow where each line consists of a pair of integers (u and v) representing an edge from u to v. (0 based indexing is used).

Output:
For each test case in a new line print the Strongly connected component of a graph. where each member of a strongly connected component is separated by a space(' ') and each strongly connected component is separated by comma (' , '). If there are many such options then print the output that will be provided by the standard implementation of the Tarjan's algorithm. (remember zero-based indexing)

Constraints:

1<=T<=10
1<=N<=20
1<=M<=200
0<=u,v<=N-1

Example:

Input:
2
3 3
1 2 2 0 2 2
4 3
1 2 2 3 3 1 
Output:
0,2,1,
0,3 2 1,

Explanation:

Testcase 2:

given no of vertex :4  --> 0,1,2,3

edges are 1-->2, 2-->3, 3-->1,

There is a connected subgraph that includes 1-2-3 which satisfy the condition of strongly connecting components i.e each node is reachable from every other nodes.

Another subgraph includes individual nodes 0. 

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Strongly connected component (Tarjans's Algo)

Output Window