Determine if Directed Graph is Strongly Connected

Given a directed graph, determine whether every vertex can reach every other vertex. Return true if the graph is strongly connected; otherwise return false. Pattern focus: SCC. This can be checked by counting SCCs or by running reachability on both the graph and its reverse.

Input Format

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

Output Format

true if the graph is strongly connected, otherwise false

Constraints

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

Examples

Example 1:

Input:

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

Output:

true

Explanation:

Every vertex can reach every other vertex.

Example 2:

Input:

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

Output:

false

Explanation:

Vertex 3 cannot reach vertex 1, so the graph is not strongly connected.

Example 3:

Input:

n = 1
edges = []

Output:

true

Explanation:

A single-vertex graph is strongly connected.

Loading...
Determine if Directed Graph is Strongly…