#C11860. Determine the Number of Strongly Connected Components

    ID: 41223 Type: Default 1000ms 256MiB

Determine the Number of Strongly Connected Components

Determine the Number of Strongly Connected Components

You are given a directed graph representing a transportation network in AlgoLand. The graph has n locations (numbered from 1 to n) and m one-way roads connecting these locations. Your goal is to find the number of strongly connected components (SCCs) in this graph.

A strongly connected component is a maximal subgraph in which any two vertices are reachable from each other. In other words, for a directed graph \(G = (V, E)\), a subset of vertices \(S \subseteq V\) forms a strongly connected component if every vertex in \(S\) can reach every other vertex in \(S\) through a sequence of edges.

Input Format Example:

5 5
1 2
2 3
3 1
4 5
5 4

In the above sample, there are 5 locations and 5 directed roads. The answer for this case is 2 since there are two strongly connected components: one containing the cycle (1, 2, 3) and one containing another cycle (4, 5).

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains two integers n and m, where n is the number of locations and m is the number of one-way roads.
  • The following m lines each contain two integers u and v representing a directed edge (road) from location u to location v.

Note: Vertices are numbered from 1 to n.

outputFormat

The output should be written to standard output (stdout) as a single integer representing the number of strongly connected components in the graph.

## sample
5 5
1 2
2 3
3 1
4 5
5 4
2