[拓扑排序][二分]Toss a Coin to Your Graph... Codeforces1679D

48 篇文章 0 订阅
23 篇文章 0 订阅
该博客探讨了一道涉及有向图、点权和操作次数限制的数学问题。题目要求通过不超过k-1次操作,使得路径上最大点权尽可能小。解决方案包括使用二分查找和拓扑排序来判断是否存在这样的路径。文章详细分析了如何构建新图、判断回路以及确定最远距离,展示了在有向图中寻找最优路径的算法思路。
摘要由CSDN通过智能技术生成

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出发程序就会认为找到了回路,实际上这是不对的。

具体代码如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值