Problems > Strongly Connected Pieces

Strongly Connected Pieces

Difficulty: 1800 4000 ms 128 MB

graphs scc

Count the strongly connected components of a directed graph (maximal sets where every node reaches every other).

Input: the first line has N and M; each of the next M lines has a directed edge u → v.

Output: the number of SCCs.

Constraints:

  • 1 ≤ N ≤ 104
  • 0 ≤ M ≤ 2×104

Sample Tests

Input 1
5 5
1 2
2 3
3 1
3 4
4 5
Output 1
3
Input 2
3 0
Output 2
3

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.