Strongly Connected Components (Kosaraju's or Tarjan's)

EXTREME · C · 5000ms

Finally, for the most critical subsystem, Rakib needs to identify tightly-coupled clusters of servers where every machine can reach every other machine in the same cluster (directed connections only). Given a directed graph with N nodes and M edges, print the number of strongly connected components (SCCs) — maximal sets of nodes where every node can reach every other node in the set.

Input

Line1: N M. Next M lines: u v (directed edge u→v).

Output

Number of strongly connected components.

Constraints

1 ≤ N ≤ 10^5, 0 ≤ M ≤ 2×10^5

Sample Input

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

Sample Output

3
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Strongly Connected Components (Kosaraju's or Tarjan's) · DIU ContestHub