CF1900E.Transitive Graph
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a directed graph G with n vertices and m edges between them.
Initially, graph H is the same as graph G . Then you decided to perform the following actions:
- If there exists a triple of vertices a , b , c of H , such that there is an edge from a to b and an edge from b to c , but there is no edge from a to c , add an edge from a to c .
- Repeat the previous step as long as there are such triples.
Note that the number of edges in H can be up to n2 after performing the actions.
You also wrote some values on vertices of graph H . More precisely, vertex i has the value of ai written on it.
Consider a simple path consisting of k distinct vertices with indexes v1,v2,…,vk . The length of such a path is k . The value of that path is defined as ∑i=1kavi .
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 H , find the one with the smallest value.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤104 ). The description of the test cases follows.
The first line of each test case contains two integers n and m ( 1≤n,m≤2⋅105 ) — the number of vertices and the number of edges.
The second line contains n integers a1,a2,…,an ( 0≤ai≤109 ) — the numbers written on the vertices of graph H .
The i -th of the next m lines contains two integers vi and ui ( 1≤vi,ui≤n ) — meaning that there is an edge going from vertex vi to vertex ui in graph G . 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 n nor the sum of m over all test cases exceeds 2⋅105 .
输出格式
For each test case, output two numbers — the length of the longest simple path in H and the minimal possible value of such path.
输入输出样例
输入#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→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 12 .
In the second test case, the longest possible path is 1→2→3→4→6→7 . As there are no longest paths with vertex 5 in them, this path has the minimal possible value of 5999999995 .
In the third test case, it can be proven that there is no path longer than 11 and that the value of the longest path cannot be less than 37 . Also, notice that the given graph has both self-loops and multiple edges.