CF1616G.Just Add an Edge

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a directed acyclic graph with nn vertices and mm edges. For all edges a→ba \to b in the graph, a<ba \lt b holds.

You need to find the number of pairs of vertices xx, yy, such that x>yx \gt y and after adding the edge x→yx \to y to the graph, it has a Hamiltonian path.

给你一个包含 nn 个顶点和 mm 条边的有向无环图(DAG)。对于图中的所有边 a→ba \to b,均满足 a<ba \lt b。

你需要找出满足以下条件的顶点对 x,yx, y 的数量:x>yx \gt y,且在图中添加边 x→yx \to y 后,该图存在哈密顿路径。

输入格式

The first line of input contains one integer tt (1≤t≤51 \leq t \leq 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 nn and mm (1≤n≤150 0001 \leq n \leq 150\,000, 0≤m≤min⁡(150 000,n(n−1)2)0 \leq m \leq \min(150\,000, \frac{n(n-1)}{2})): the number of vertices and edges in the graph.

Each of the next mm lines contains two integers aa, bb (1≤a<b≤n1 \leq a \lt b \leq n), specifying an edge a→ba \to b in the graph. No edge a→ba \to b appears more than once.

输入的第一行包含一个整数 tt(1≤t≤51 \leq t \leq 5):测试用例的数量。

接下来的若干行包含各测试用例的描述。

第一行给出两个整数 nn 和 mm(1≤n≤150 0001 \leq n \leq 150\,000,0≤m≤min⁡(150 000,n(n−1)2)0 \leq m \leq \min(150\,000, \frac{n(n-1)}{2})):图中顶点和边的数量。

接下来的 mm 行每行包含两个整数 aa、bb(1≤a<b≤n1 \leq a \lt b \leq n),表示图中的一条有向边 a→ba \to b。任意边 a→ba \to b 不会重复出现。

输出格式

For each test case, print one integer: the number of pairs of vertices xx, yy, x>yx \gt y, such that after adding the edge x→yx \to y to the graph, it has a Hamiltonian path.

对于每个测试用例,输出一个整数:满足条件的顶点对 xx、yy(其中 x>yx \gt y)的个数,使得在图中添加有向边 x→yx \to 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→yx \to y such that x>yx \gt y is valid, because there already is a path 1→2→31 \to 2 \to 3.

In the second example only the edge 4→14 \to 1 is valid. There is a path 3→4→1→23 \to 4 \to 1 \to 2 if this edge is added.

In the third example you can add edges 2→12 \to 1, 3→13 \to 1, 4→14 \to 1, 4→24 \to 2.

在第一个例子中,任意满足 x>yx \gt y 的边 x→yx \to y 均合法,因为已存在路径 1→2→31 \to 2 \to 3。

在第二个例子中,仅有边 4→14 \to 1 合法。若添加该边,则存在路径 3→4→1→23 \to 4 \to 1 \to 2。

在第三个例子中,你可以添加边 2→12 \to 1、3→13 \to 1、4→14 \to 1、4→24 \to 2。

输入解题思路,AI测评打分。不知道怎么写?

首页