OCC '19 S6 - City Tolls
View as PDF has recently been teleported to a strange new world. Here violence is common and the world has been split into tiny city-states. As such, to enter each city  will have to pay a certain entrance fee. Moreover, the only safe way to move through this world is through the roads which connect the cities. However, because cities are very careful, each city will be connected to at most 5 other cities.  being new to this world, and thinking ahead, has  queries for you to calculate the danger of the least dangerous path from city 
 to city 
. Because  would not like to be robbed, he wants to pay as little as possible at each city to not seem rich. As such,  defines the danger of a path as the largest entrance fee that he will have to pay on that path.
Note: For each query,  will start at city  and as such will not need to pay the entrance fee of city 
.
Constraints
Each city will be connected to at most 5 other cities.
No city will have a road to itself, and there will only be at max one road between any two cities.
Subtask 1 [15%]
Subtask 2 [30%]
The road network will always be a tree.
Subtask 3 [55%]
No additional constraints.
Input Specification
The first line of input contains , 
, and 
 representing the number of cities, roads, and queries respectively.
The next line contains  integers 
 indicating the entrance cost of each city.
The next  lines contain two integers 
 and 
 denoting a road from city 
 to 
.
The next  lines contain two integers 
 and 
 requesting the danger of the least dangerous path from city 
 to city 
.
Output Specification
For each query , 
, output the danger of the least dangerous path from city 
 to city 
, and if there is no valid path, output 
-1.
Sample Input
6 7 5
4 3 2 5 1 3
1 2
2 5
3 1
4 2
5 1
5 4
4 3
1 4
5 3
2 3
1 5
1 6
Sample Output
5
4
4
1
-1
Explanation
One of the least dangerous paths from city  to city 
 is 
 paying entrance fees of 
 and 
 for cities 
 and 
 respectively, and having a danger of 
.
One of the least dangerous paths from city  to city 
 is 
 paying entrance fees of 
 and 
 for cities 
 and 
 respectively, and having a danger of 
.
One of the least dangerous paths from city  to city 
 is 
 paying entrance fees of 
 and 
 for cities 
 and 
 respectively, and having a danger of 
.
One of the least dangerous paths from city  to city 
 is 
 paying an entrance fee of 
 for city 
, and having a danger of 
.
There is no valid path from city  to city 
, thus the output is 
-1.
Comments