The Great KL
Practice
3.6 (25 votes)
Medium
Tree
Greedy algorithm
Problem
64% Success 844 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

View Russian Translation

The Great KL lives in city KLand. KLand has n junctions and n-1 bidirectional roads. One can reach any city from any other city using only these roads.

Each junction has a cupid in it. Cupids are of m different types. Type of the cupid in junction i is ci. KL wants to know for each of m types that what is the maximum distance between two cupids of that type. Distance between two junctions is the length of their shortest path (number of roads in the shortest path between them).

KL is not that smart and can't figure this out himself. Please help him!

Input

The first line of input contains two integers n and m (1 ≤ m ≤ n ≤ 105).

The second line contains n integers c1, c2, ..., cn separated by spaces (1 ≤ ci ≤ m).

The next n-1 lines contain the roads. Each line contains two integers v and u; Endpoints of a road (1 ≤ v, u ≤ n).

For each i between 1 to m there is at least one cupid whit that type.

Output

Print m integers in one line; i-th of them should be the answer for cupid-type i.

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:20
7 votes
Tags:
Basic ProgrammingBasics of ImplementationEasyHash MapsImplementationString Manipulation
Points:30
Tags:
Grammar-VerifiedOpenGrammar-VerifiedOpenGraph TheoryBreadth-first searchGraphQueueMediumAlgorithmsGrammar-Verified