Kosaraju Component Count

Given a directed graph, count its strongly connected components using Kosaraju's algorithm. Pattern focus: Strongly Connected Components. This is the counting form of the SCC decomposition.

Input Format

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

Output Format

number of SCCs

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:

3

Explanation:

The SCCs are {1,2,3}, {4}, and {5}.

Example 2:

Input:

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

Output:

2

Explanation:

There are two SCCs.

Example 3:

Input:

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

Output:

3

Explanation:

Every vertex is its own SCC in a one-way chain.

Loading...
Kosaraju Component Count - Advanced Graphs