List Strongly Connected Components

Given a directed graph, return all of its strongly connected components. Each component should contain the vertices that are mutually reachable, and the components should be returned in the order produced by a standard SCC traversal. Pattern focus: SCC. Build the component list rather than only counting components.

Input Format

n = number of vertices, edges = directed edges [u, v]

Output Format

all SCCs, each SCC sorted ascending, and components ordered by Kosaraju's second pass

Constraints

  • 1 <= n <= 10^5
  • 1 <= edges.length <= 2 * 10^5

Examples

Example 1:

Input:

n = 5
edges = [[1,2],[2,3],[3,1],[3,4],[4,5]]

Output:

[[1,2,3],[4],[5]]

Explanation:

The graph splits into three SCCs.

Example 2:

Input:

n = 4
edges = [[1,2],[2,1],[2,3],[3,4],[4,3]]

Output:

[[1,2],[3,4]]

Explanation:

There are two SCCs: one pair and another pair.

Example 3:

Input:

n = 6
edges = [[1,2],[2,3],[3,1],[4,5],[5,4],[5,6]]

Output:

[[1,2,3],[4,5],[6]]

Explanation:

The first three nodes form one SCC, the next two form another, and node 6 is alone.

Loading...
List Strongly Connected Components