Escaped from NEF
시간 제한2초메모리 제한512 MB
방향 그래프의 기저 무방향 그래프가 선인장 그래프일 때, x에서 y로 가는 방향 경로가 존재하는 순서쌍 (x, y)의 개수를 구한다.
문제
A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. Multiedges (multiple edges between a pair of vertices) and loops (edges that connect a vertex to itself) are not allowed in a cactus.
You are given a directed graph with vertices with the following property. Consider an undirected graph with vertices built as follows: for each directed edge in , add an undirected edge to . Then is a cactus.
Find the number of ordered pairs of vertices such that there exists a path from vertex to vertex in . Assume that a path from a vertex to itself always exists.
입력
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains two integers and , denoting the number of vertices and the number of edges in (; ).
Each of the next lines contains two integers and , denoting an edge in directed from to (; ).
The undirected graph consisting of undirected edges is a cactus.
It is guaranteed that the sum of over all test cases does not exceed .
출력
For each test case, print the number of ordered pairs such that vertex is reachable from vertex in .