CF1616G.Just Add an Edge
NOI/NOI+/CTSC
通过率:0%
时间限制:1.00s
内存限制:256MB
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.
给你一个包含 n 个顶点和 m 条边的有向无环图(DAG)。对于图中的所有边 a→b,均满足 a<b。
你需要找出满足以下条件的顶点对 x,y 的数量:x>y,且在图中添加边 x→y 后,该图存在哈密顿路径。
输入格式
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.
输入的第一行包含一个整数 t(1≤t≤5):测试用例的数量。
接下来的若干行包含各测试用例的描述。
第一行给出两个整数 n 和 m(1≤n≤150000,0≤m≤min(150000,2n(n−1))):图中顶点和边的数量。
接下来的 m 行每行包含两个整数 a、b(1≤a<b≤n),表示图中的一条有向边 a→b。任意边 a→b 不会重复出现。
输出格式
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.
对于每个测试用例,输出一个整数:满足条件的顶点对 x、y(其中 x>y)的个数,使得在图中添加有向边 x→y 后,该图存在哈密顿路径。
输入输出样例
输入#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.
在第一个例子中,任意满足 x>y 的边 x→y 均合法,因为已存在路径 1→2→3。
在第二个例子中,仅有边 4→1 合法。若添加该边,则存在路径 3→4→1→2。
在第三个例子中,你可以添加边 2→1、3→1、4→1、4→2。
输入解题思路,AI测评打分。不知道怎么写?