#P6111. [USACO18JAN] MooTube S
[USACO18JAN] MooTube S
Description
In his spare time, Farmer John created a new video-sharing service, which he named MooTube. On MooTube, Farmer John’s cows can record, share, and discover many interesting videos. His cows have posted videos (), numbered for convenience. However, FJ cannot figure out how to help his cows find new videos they might like.
FJ wants to create a “recommended videos” list for each MooTube video. In this way, cows will be recommended videos that are most related to the videos they have already watched.
FJ designed a “relevance” measure, as the name suggests, to determine how related two videos are. He chose pairs of videos and manually computed the relevance between each pair. Then, FJ built his videos into a tree, where each video is a node, and he manually connected the pairs of videos. For convenience, FJ chose these pairs so that any video can reach any other video via a connected path. FJ decides to define the relevance between any two videos as the minimum relevance of any edge along this path.
Farmer John wants to choose a value so that, next to any given MooTube video, all other videos with relevance at least to that video will be recommended. However, FJ worries that he might recommend too many videos to his cows, which could distract them from producing milk. Therefore, he wants to set an appropriate value of . Farmer John would like your help in answering some queries about the recommended videos for different values of .
Input Format
The first line contains and ().
The next lines describe the pairs of videos that FJ compared manually. Each line contains three integers , , and (, ), indicating that videos and are connected with relevance .
The next lines describe Farmer John’s queries. Each line contains two integers and (, ), meaning that in the -th query, FJ asks: when , how many videos will appear in the recommended list for video .
Output Format
Output lines. On the -th line, output the answer to FJ’s -th query.
4 3
1 2 3
2 3 2
2 4 4
1 2
4 1
3 1
3
0
2
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号