One day Masha was walking in the park and found a graph under a tree... Surprised? Did you think that this problem would have some logical and reasoned story? No way! So, the problem...
Masha has an oriented graph which ii-th vertex contains some positive integer aiai. Initially Masha can put a coin at some vertex. In one operation she can move a coin placed in some vertex uu to any other vertex vv such that there is an oriented edge u→vu→v in the graph. Each time when the coin is placed in some vertex ii, Masha write down an integer aiai in her notebook (in particular, when Masha initially puts a coin at some vertex, she writes an integer written at this vertex in her notebook). Masha wants to make exactly k−1k−1 operations in such way that the maximum number written in her notebook is as small as possible.
Input
The first line contains three integers nn, mm and kk (1≤n≤2⋅1051≤n≤2⋅105, 0≤m≤2⋅1050≤m≤2⋅105, 1≤k≤10181≤k≤1018) — the number of vertices and edges in the graph, and the number of operation that Masha should make.
The second line contains nn integers aiai (1≤ai≤1091≤ai≤109) — the numbers written in graph vertices.
Each of the following mm lines contains two integers uu and vv (1≤u≠v≤n1≤u≠v≤n) — it means that there is an edge u→vu→v in the graph.
It's guaranteed that graph doesn't contain loops and multi-edges.
Output
Print one integer — the minimum value of the maximum number that Masha wrote in her notebook during optimal coin movements.
If Masha won't be able to perform k−1k−1 operations, print −1−1.
Examples
input
6 7 4 1 10 2 3 4 5 1 2 1 3 3 4 4 5 5 6 6 2 2 5
output
4
input
6 7 100 1 10 2 3 4 5 1 2 1 3 3 4 4 5 5 6 6 2 2 5
output
10
input
2 1 5 1 1 1 2
output
-1
input
1 0 1 1000000000
output
1000000000
题意: 给出n个点,m条有向边,以及一个参数k,每个点有一个点权,可以任选一个起点,并从该起点走k-1步,输出路径上最大值的最小值。
分析: 二分答案,首先二分出一个x,check中检测是否能不经过大于x的点走完k-1步,而check的具体实现则需要重新建图,只考虑那些点权小于等于x的点,如果新图中出现了回路,那么显然一定可以return true,如果没有回路那新图就是一个DAG(有向无环图),直接拓扑排序求最远距离,如果最远距离大于等于k-1,那就可以return true,否则return false。
而判回路可以直接用拓扑排序一起做了,我当时想的是用dfs来判环,这个方法对于无向图是可以的,但是在有向图中就不能用dfs了,比如这么一个图1->2, 3->2,从1出发会标记2,然后从3出发程序就会认为找到了回路,实际上这是不对的。
具体代码如下: