407404: GYM102784 K Territorial Tarantulas

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

K. Territorial Tarantulastime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Farmer John (a.k.a. FJ) has gotten bored of breeding cows, and has decided to move onto raising tarantulas on his farm to reap a gigantic profit in Spooky Season (many of his clients are Halloween pranksters). He notices that the $$$N$$$ spiders have built individual burrows (yes, tarantulas build burrows instead of webs) to live in. However, he also notices that there are $$$N-1$$$ bidirectional tunnels connecting these burrows such that any tarantula could travel from his/her nest to any other nest. It turns out that there are subspecies of tarantulas among the ones that FJ has chosen to nurture, and they are always competing amongst each other to cover the most "range" over the network of tunnels. The range of a subspecies is defined as the longest path (calculated as the number of tunnels taken) between any two burrows belonging to members of the same subspecies. If there are not at least two members of the same subspecies present on the farm, then the range of that subspecies is $$$0$$$. FJ wants to calculate the range for every subspecies present in his farm, and promises you a cut of the sales if you help him out.

Input

The first line of input contains two integers $$$N$$$ ($$$1 \leq N \leq 200000$$$) and $$$K$$$ ($$$1 \leq K \leq N$$$) representing the number of tarantulas and the number of subspecies, respectively. The second line of input contains $$$N$$$ integers $$$s_i$$$ ($$$1 \leq s_i \leq K$$$) representing what subspecies the $$$i$$$-th tarantula falls under. The next $$$N-1$$$ lines each contain two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq N$$$) to represent a tunnel from the burrow of spider $$$a$$$ to the burrow of spider $$$b$$$.

Output

Output $$$K$$$ lines of output, with one integer on each line representing the range of the $$$i$$$-th subspecies of tarantula.

ExampleInput
6 2
1 2 1 2 2 1
3 1
1 2
1 4
1 5
5 6
Output
3
2

加入题单

算法标签: