#BS0000445. Di chuyển đồng xu trên đồ thị (Toss a Coin to Your Graph...)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Di chuyển đồng xu trên đồ thị (Toss a Coin to Your Graph...)
Toss a Coin to Your Graph...
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Masha has a directed graph with vertices numbered from to and edges. Vertex bears a positive integer .
She places a coin on any vertex and records its value. Each subsequent move follows a directed edge from her current vertex and records . Vertices may be visited repeatedly, recording their values on every visit.
She must make exactly moves, recording exactly numbers. Find the smallest possible maximum among the recorded numbers.
Input
- The first line contains integers : the vertex count, edge count and number of values to record.
- The second line contains integers , the vertex values in order.
- Each of the next lines contains integers , denoting a directed edge from to .
Output
Print one integer: the smallest attainable maximum, or -1 if the required number of moves is impossible.
Subtasks
- Subtask 1 (20 points): ; ; ; ; , ; for all , ; no repeated ordered pairs of endpoints.
- Subtask 2 (30 points): ; ; ; ; , ; for all , ; no repeated ordered pairs of endpoints.
- Subtask 3 (50 points): ; ; ; ; , ; for all , ; no repeated ordered pairs of endpoints.
Examples

Example 1
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
Explanation
Follow , recording . With threshold 3, only vertices 1, 3, 4 remain, forming a chain of at most three vertices, insufficient for four records. Thus 4 is optimal.
Example 2
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
Explanation
Repeat for 33 rounds, making 99 moves. The maximum is 10. Below 10, vertex 2 is unavailable and the remaining chain has at most five vertices, insufficient for 100 records.
Example 3
Input
2 1 5
1 1
1 2
Output
-1
Explanation
The only edge is from 1 to 2, allowing at most one move instead of four.
Example 4
Input
1 0 1
1000000000
Output
1000000000
Explanation
Only one value is required, with no moves. The sole vertex has value .