CF1616G.Just Add an Edge

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

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

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

输入格式

The first line of input contains one integer tt ( 1t51 \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 ( 1n1500001 \leq n \leq 150\,000 , 0mmin(150000,n(n1)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 ( 1a<bn1 \leq a < b \leq n ), specifying an edge aba \to b in the graph. No edge aba \to b appears more than once.

输出格式

For each test case, print one integer: the number of pairs of vertices xx , yy , x>yx > y , such that after adding the edge xyx \to 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 xyx \to y such that x>yx > y is valid, because there already is a path 1231 \to 2 \to 3 .

In the second example only the edge 414 \to 1 is valid. There is a path 34123 \to 4 \to 1 \to 2 if this edge is added.

In the third example you can add edges 212 \to 1 , 313 \to 1 , 414 \to 1 , 424 \to 2 .

首页