A directed graph consists of a set of vertices and a set of arcs . An arc is an ordered pair , where . A path from a vertex to a vertex is a sequence of arcs . By definition, there is always a path from every vertex to itself.
Consider the following equivalence relation: two vertices and of are related if, and only if, there is a path from to and a path from to . Every equivalence class resulting from this definition is called a strongly connected component of .
Given a directed graph, calculate how many strongly connected components it has.
Input begins with the number of cases. Each case consists of the number of vertices and the number of arcs , followed by pairs . Vertices are numbered starting at 0. There are not repeated arcs, nor self-arcs . Assume .
For every graph, print its number of strongly connected components.
Author: Xavier Martínez
Generation: 2026-01-25T11:53:46.637Z
© Jutge.org, 2006–2026.
https://jutge.org