CF1900E.Transitive Graph

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a directed graph GG with nn vertices and mm edges between them.

Initially, graph HH is the same as graph GG. Then you decided to perform the following actions:

  • If there exists a triple of vertices aa, bb, cc of HH, such that there is an edge from aa to bb and an edge from bb to cc, but there is no edge from aa to cc, add an edge from aa to cc.
  • Repeat the previous step as long as there are such triples.

Note that the number of edges in HH can be up to n2n^2 after performing the actions.

You also wrote some values on vertices of graph HH. More precisely, vertex ii has the value of aia_i written on it.

Consider a simple path consisting of kk distinct vertices with indexes v1,v2,…,vkv_1, v_2, \ldots, v_k. The length of such a path is kk. The value of that path is defined as ∑i=1kavi\sum_{i = 1}^k a_{v_i}.

A simple path is considered the longest if there is no other simple path in the graph with greater length.

Among all the longest simple paths in HH, find the one with the smallest value.

给你一个包含 nn 个顶点和 mm 条有向边的有向图 GG。

初始时,图 HH 与图 GG 完全相同。随后你决定执行如下操作:

  • 若图 HH 中存在三个顶点 aa、bb、cc,使得存在从 aa 到 bb 的边以及从 bb 到 cc 的边,但不存在从 aa 到 cc 的边,则添加一条从 aa 到 cc 的边。
  • 只要存在满足上述条件的三元组,就重复执行上一步。

注意:执行完上述操作后,图 HH 中的边数最多可达 n2n^2。

你还为图 HH 的各个顶点写上了一些数值。更准确地说,顶点 ii 上写的数值为 aia_i。

考虑一条由 kk 个互不相同的顶点组成的简单路径,其顶点编号依次为 v1,v2,…,vkv_1, v_2, \ldots, v_k。该路径的长度定义为 kk;该路径的值定义为 ∑i=1kavi\sum_{i = 1}^k a_{v_i}。

若图中不存在长度更大的简单路径,则称该简单路径为最长简单路径。

在图 HH 的所有最长简单路径中,找出值最小的一条。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤2⋅1051 \le n,m \le 2 \cdot 10^5) — the number of vertices and the number of edges.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤1090 \le a_i \le 10^9) — the numbers written on the vertices of graph HH.

The ii-th of the next mm lines contains two integers viv_i and uiu_i (1≤vi,ui≤n1 \le v_i, u_i \le n) — meaning that there is an edge going from vertex viv_i to vertex uiu_i in graph GG. Note that edges are directed. Also note that the graph may have self-loops and multiple edges.

It is guaranteed that neither the sum of nn nor the sum of mm over all test cases exceeds 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤2⋅1051 \le n,m \le 2 \cdot 10^5)—— 分别表示图 HH 的顶点数和边数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤1090 \le a_i \le 10^9)—— 表示图 HH 各顶点上所写的数字。

接下来的 mm 行中,第 ii 行包含两个整数 viv_i 和 uiu_i(1≤vi,ui≤n1 \le v_i, u_i \le n)—— 表示图 GG 中存在一条从顶点 viv_i 指向顶点 uiu_i 的有向边。注意:边是有向的;此外,图可能包含自环和重边。

保证所有测试用例的 nn 之和以及 mm 之和均不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output two numbers — the length of the longest simple path in HH and the minimal possible value of such path.

对于每个测试用例,输出两个数——图 HH 中最长简单路径的长度,以及该长度下路径的最小可能值。

输入输出样例

  • 输入#1

    3
    5 6
    2 2 4 1 3
    1 2
    1 3
    2 4
    3 4
    4 5
    5 2
    7 7
    999999999 999999999 999999999 999999999 1000000000 999999999 1000000000
    1 2
    2 3
    3 4
    4 1
    4 5
    4 6
    6 7
    14 22
    2 3 5 7 3 4 1 4 3 4 2 2 5 1
    1 2
    2 3
    2 4
    3 1
    4 4
    4 5
    5 6
    5 6
    5 12
    6 7
    6 8
    7 5
    7 7
    7 9
    8 4
    9 11
    10 9
    11 10
    11 10
    12 13
    13 14
    14 12

    输出#1

    5 12
    6 5999999995
    11 37

说明/提示

In the first test case, the longest path in both graphs is 1→3→4→5→21 \to 3 \to 4 \to 5 \to 2. As the path includes all vertices, the minimal possible value of the longest path is the sum of values on all vertices, which is 1212.

In the second test case, the longest possible path is 1→2→3→4→6→71 \to 2 \to 3 \to 4 \to 6 \to 7. As there are no longest paths with vertex 55 in them, this path has the minimal possible value of 5 999 999 9955\,999\,999\,995.

In the third test case, it can be proven that there is no path longer than 1111 and that the value of the longest path cannot be less than 3737. Also, notice that the given graph has both self-loops and multiple edges.

在第一个测试用例中,两个图中的最长路径均为 1→3→4→5→21 \to 3 \to 4 \to 5 \to 2。由于该路径包含了所有顶点,因此最长路径的最小可能值即为所有顶点上数值之和,即 1212。

在第二个测试用例中,可能的最长路径为 1→2→3→4→6→71 \to 2 \to 3 \to 4 \to 6 \to 7。由于不存在包含顶点 55 的最长路径,因此该路径具有最小可能值 5 999 999 9955\,999\,999\,995。

在第三个测试用例中,可以证明不存在长度超过 1111 的路径,且最长路径的值不可能小于 3737。此外,请注意所给图中既存在自环,也存在重边。

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

首页