Tree Path
Practice
4 (4 votes)
C++
Graph representation
Algorithms
Graphs
Problem
60% Success 1269 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given an undirected tree with N nodes. Every node has a weight assigned to it denoted by array W.

Among all the simple paths of length K in the given tree, Sam can choose to pick any path and traverse on it.

Help Sam choose a path such that the maximum weight node present on the path is as minimum as possible.

Return -1, if no such path exists.

Note:

  • Length of the simple path, is defind by the number of edges present on the path.

Input

  • First line contains two space-separated integers N K.
  • Second line contains N space-separated integers denoting the array W.
  • Next N - 1 lines contains two space separated integers u v, denoting an edge between node u and v.

Output

Print an integer denoting the maximum weight node present on the path traversed by Sam.

Constraints

\(1 \le N \le 10^5 \\ 1 \le K \le N \\ 1 \leq u, v \leq N \\ 1 \le W[i] \le 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:50
5 votes
Tags:
AlgorithmsApprovedGraphsHardOpen
Points:50
2 votes
Tags:
GraphsAlgorithmsGraph Representation
Points:50
18 votes
Tags:
GraphsHard