Sightseeing Walk
Practice
3.8 (30 votes)
Depth first search
Easy
Graphs
Problem
24% Success 2347 Attempts 30 Points 3s Time Limit 256MB Memory 1024 KB Max Code

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 \(H_i\). 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 \(a_i\) to point \(b_i\). 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 \(H_{finish} - H_{start}\) is maximized.

Your task is to help him and tell him the maximum possible \(H_{finish} - H_{start}\) .

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, \(H_i\). Next M lines contains 2 numbers \(a_i\) and \(b_i\)

Output format:

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

Constraints:

  • \(1 \le T \le 10\)
  • \(0 \le H_i \le 10^9\)
  • \(1 \le a_i,b_i \le N\)
  • (20 points): \(1 \le N \le 1000, 1 \le M \le 2000\)
  • (80 points): \(1 \le N \le 10^5, 1 \le M \le 2 \cdot 10^5\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
9 votes
Tags:
Depth First SearchEasyGraphsImplementationRecursion
Points:30
19 votes
Tags:
AlgorithmsDepth First SearchEasyGraphs
Points:30
4 votes
Tags:
Bit ManipulationDepth First SearchEasyGraphs