eolymp
bolt
Try our new interface for solving problems
Problems

MooTube (Silver)

MooTube (Silver)

In his spare time, Farmer John has created a new video-sharing service, which he names MooTube. On MooTube, Farmer John's cows can record, share, and discover many amusing videos. His cows already have posted $n$ videos, conveniently numbered $1 ... n$. However, FJ can't quite figure out how to help his cows find new videos they might like. FJ wants to create a list of "suggested videos" for every MooTube video. This way, cows will be recommended the videos most relevant to the ones they already watch. FJ devises a metric of "relevance", which determines, as the name suggests, how relevant two videos are to each other. He picks $n − 1$ pairs of videos and manually computes their pairwise relevance. Then, FJ visualizes his videos as a network, where each video is a node and the $n − 1$ pairs of videos he manually considered are connected. Conveniently, FJ has picked his $n − 1$ pairs so that any video can be reached from any other video along a path of connections in exactly one way. FJ decides that the relevance of any pair of videos should be defined as the minimum relevance of any connection along this path. Farmer John wants to pick a value $k$ so that next to any given MooTube video, all other videos with relevance at least $k$ to that video will be suggested. However, FJ is worried that too many videos will be suggested to his cows, which could distract them from milk production! Therefore, he wants to carefully set an appropriate value of $k$. Farmer John would like your help answering a number of questions about the suggested videos for certain values of $k$. \InputFile The first line contains $n\:(1 \le n \le 5000)$ and $q\:(1 \le q \le 5000)$. The next $n − 1$ lines each describe a pair of videos FJ manually compares. Each line includes three integers $p_i, q_i$ and $r_i\:(1 \le p_i, q_i \le n, 1 \le r_i \le 10^9)$, indicating that videos $p_i$ and $q_i$ are connected with relevance $r_i$. The next $q$ lines describe Farmer John's $q$ questions. Each line contains two integers $k_i$ and $v_i\:(1 \le k_i \le 10^9, 1 \le v_i \le n)$, indicating that FJ's $i$-th question asks how many videos will be suggested to viewers of video $v_i$ if $k = k_i$. \OutputFile Output $q$ lines. On line $i$, output the answer to FJ's $i$-th question. \includegraphics{https://static.eolymp.com/content/hl/hlkqiqpkj971ndt9aa922feggg.gif} \Examples Farmer John finds that videos one and two have relevance three, that videos two and three have relevance two, and that videos two and four have relevance four. Based on this, videos one and three have relevance $min(3, 2) = 2$, videos one and four have relevance $min(3, 4) = 3$, and videos three and four have relevance $min(2, 4) = 2$. Farmer John wants to know how many videos will be suggested from video two if $k = 1$, from video one if $k = 3$, and from video one if $k = 4$. We see that with $k = 1$, videos $1, 3$ and $4$ will be suggested on video two. With $k = 4$, no videos will be suggested from video one. With $k = 3$, however, videos $2$ and $4$ will be suggested from video one.
Time limit 2 seconds
Memory limit 128 MiB
Input example #1
4 3
1 2 3
2 3 2
2 4 4
1 2
4 1
3 1
Output example #1
3
0
2
Source 2018 USACO January, Silver