Sightseeing Walk

3.8

30 votes
Depth First Search, Easy, Graphs
Problem

Kevin wants to have a spectacular hiking tour over the mountains.

Now he needs to plan his route. Kevin knows N sightseeing points. Height of the i-th point equals Hi. Also there are M lanes connecting these points. These lanes are designed in such a way that using the i-th lane Kevin can walk from point ai to point bi. Note that he can't walk in the opposite direction using this lane but may walk using some other lane.

Kevin thinks that the path is the most spectacular if the height difference of his route is the largest. So he needs to choose start and finish points such that it is possible to reach finish from start and HfinishHstart is maximized.

Your task is to help him and tell him the maximum possible HfinishHstart .

Input format:

The first line of input will contain an integer T, denoting the number of test cases.
Each test case starts with 2 numbers N and M. Next line contains N numbers, Hi. Next M lines contains 2 numbers ai and bi

Output format:

For every test case output one number - maximum height difference of the path.

Constraints:

  • 1T10
  • 0Hi109
  • 1ai,biN
  • (20 points): 1N1000,1M2000
  • (80 points): 1N105,1M2105.
Sample Input
1
4 5
2 4 10 7
2 4
1 2
3 1
3 4
4 1
Sample Output
5
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

Kevin's route is 124. Height difference equals H4H1=5.

Editor Image

?