Strongly Connected Components Count

HardGraphDFSMathLinked List

Description

Given a directed graph with n nodes (0..n-1) and a list of directed edges, return the number of strongly connected components.

Examples

Input:n = 5, edges = [[0,1],[1,2],[2,0],[3,4]]
Output:3
Explanation:

Strongly connected components are {0,1,2}, {3}, and {4}, giving a count of 3.

Input:n = 1, edges = []
Output:1
Explanation:

A single node is its own strongly connected component, so the count is 1.

Input:n = 3, edges = [[0,1],[1,2]]
Output:3
Explanation:

The DAG 0->1->2 has three trivial single-node SCCs.

Constraints

  • 1 ≤ n ≤ 1000
  • 0 ≤ edges.length ≤ 5000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!