CF1616G.Just Add an Edge
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a directed acyclic graph with n vertices and m edges. For all edges a→b in the graph, a<b holds.
You need to find the number of pairs of vertices x , y , such that x>y and after adding the edge x→y to the graph, it has a Hamiltonian path.
输入格式
The first line of input contains one integer t ( 1≤t≤5 ): the number of test cases.
The next lines contains the descriptions of the test cases.
In the first line you are given two integers n and m ( 1≤n≤150000 , 0≤m≤min(150000,2n(n−1)) ): the number of vertices and edges in the graph.
Each of the next m lines contains two integers a , b ( 1≤a<b≤n ), specifying an edge a→b in the graph. No edge a→b appears more than once.
输出格式
For each test case, print one integer: the number of pairs of vertices x , y , x>y , such that after adding the edge x→y to the graph, it has a Hamiltonian path.
输入输出样例
输入#1
3 3 2 1 2 2 3 4 3 1 2 3 4 1 4 4 4 1 3 1 4 2 3 3 4
输出#1
3 1 4
说明/提示
In the first example, any edge x→y such that x>y is valid, because there already is a path 1→2→3 .
In the second example only the edge 4→1 is valid. There is a path 3→4→1→2 if this edge is added.
In the third example you can add edges 2→1 , 3→1 , 4→1 , 4→2 .