Seunghyun and Seunghyun
Time limit2sMemory limit256 MB
For each query (S, E), find the minimum possible maximum value of C[a]*C[b] over calls made while the two travelers swap cities.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Greedy
- Solved
- No attempts yet
Problem
In the land of Seokhwan there are two people named Seunghyun. To tell them apart, call one of them Seunghyun 13 and the other Seunghyun 16. Neither knew the other existed until a news story told them so a while ago. Finding it strange that someone shared their name, they tracked down each other's number and started talking often.
One day each of them got curious about the city the other lives in. Describing a city over the phone wore them out, so they decided to travel to each other's city instead.
Seokhwan has cities numbered 1 through , and roads run between them. One road joins two different cities and can be walked in both directions. Starting from any city, you can follow roads and reach every other city.
Seunghyun 13 is in city and Seunghyun 16 lives in city . To explain the way to their own city, they keep the phone call up and travel like this.
- On day 0, Seunghyun 13 is in city and Seunghyun 16 is in city .
- On the morning of day () they call each other and decide who moves that day. Only one of them may move on a day.
- The one who moves picks a road connected to the city they are in, follows it, and arrives at the city on the other end. This move always finishes before sunset.
- After sunset on day they call again and check that both are safe.
- If Seunghyun 13 is in city and Seunghyun 16 is in city right after that check, the trip ends. Otherwise they sleep at an inn and go back to step 2.
A call needs a phone that can put out at least a certain radio signal power. Each city has a positive integer that says how well radio signals spread there, and a call between city and city needs a phone that can put out at least . Strange as it is, the rule still applies when both of them are inside the same city.
Before the trip starts, the two Seunghyuns each buy one identical phone and use only that phone until the trip ends. A phone costs in proportion to the output it can put out, so the way they travel changes how expensive the phone has to be. Given and , find the smallest phone output that lets them finish the trip safely.
Input
The input is a single test case.
The first line has the number of cities () and the number of roads ().
The second line has integers. The -th integer is (), the radio signal constant of city .
Each of the next lines has two integers and (, ) separated by a space, meaning a road joins city and city .
The next line has the number of questions (). Each of the next lines has two integers and (, ) separated by a space, meaning Seunghyun 13 starts in city and Seunghyun 16 starts in city .
Output
Print the answers to the questions on lines, in the given order. On the -th line print the smallest phone output that finishes the trip safely for the -th question.