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.
n = number of vertices, edges = directed edges [u, v]
number of SCCs
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.