eolymp
bolt
Try our new interface for solving problems
Məsələlər

Transitive Closure

Transitive Closure

In almost all ICPC trainings, one of the basic items to be taught (or even better, assumed) is how to compute the transitive closure of a directed graph. Since this concept is so elementary, there is a professor that suggests to solve this problem using Warshall's algorithm. As one of the bonus tasks of this ICPC, we would like to examine whether the participants have indeed mastered this technique. Given a directed graph \textbf{G = < V, E >} with \textbf{N = |V|} vertices (\textbf{2} ≤ \textbf{N} ≤ \textbf{2500}) and \textbf{M = |E|} edges (\textbf{1} ≤ \textbf{M} ≤ \textbf{10000}), its transitive closure is the binary relation \textbf{ R} on \textbf{V} such that two vertices, \textbf{A} and \textbf{B}, are related in \textbf{R} if there is a directed path from \textbf{A} to \textbf{B}, where a directed path is a sequence of edges of the graph \textbf{C_0C_1}, \textbf{C_1C_2}, ..., \textbf{C_\{K-1\}C_K} such that \textbf{A = C_0} and \textbf{B = C_K}. Your task is to compute \textbf{R}. Because \textbf{R} might be very large, we've decided to simplify the output. Calculate the number of ordered pairs of (\textbf{X}, \textbf{Y}) where \textbf{X} is not equal to \textbf{Y} such that there is a path from vertex \textbf{X} to vertex \textbf{Y} in graph \textbf{G}. \InputFile The first line of input contains an integer \textbf{T} (\textbf{T} ≤ \textbf{10}), denoting the number of testcases. The following lines describe each test case of the following format. The first line of a testcase consists of two integers \textbf{N} and \textbf{M}. Each of the following \textbf{M} lines contains \textbf{2} integers, \textbf{A} and \textbf{B}, denoting that there is a directed edge from \textbf{A} to \textbf{B}. The vertices are numbered from \textbf{1} to \textbf{N}, and there will be no repeated edges within a description of a graph. \OutputFile For each case, output an integer denoting the number of ordered pairs of (\textbf{X}, \textbf{Y}) where \textbf{ X} is not equal to \textbf{Y} such that there is a path from vertex \textbf{X} to vertex \textbf{Y}.
Zaman məhdudiyyəti 3 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
2
3 2
1 2
2 3
4 4
1 2
2 3
2 4
4 1
Çıxış verilənləri #1
3
9
Mənbə ACM-ICPC 2010 Jakarta