#P4185. [USACO18JAN] MooTube G
[USACO18JAN] MooTube G
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 (), conveniently numbered . 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. This way, cows will be recommended videos that are most related to the videos they have already watched.
FJ designed a “relevance” metric that, as the name suggests, measures how related two videos are. FJ models his videos as a tree (where each node represents a video) and computes the relevance for all pairs of adjacent videos. In this way, any video can reach any other video via a connected path. FJ defines the relevance of any pair of videos as the minimum relevance along the edges on the path between them.
Farmer John wants to choose a value so that, next to any given MooTube video, he will recommend all other videos whose relevance to that video is at least . However, FJ worries about recommending too many videos, which might distract the cows from producing milk. Therefore, he wants to set an appropriate value of . Farmer John needs your help to answer some questions about the recommended videos for given values.
Input Format
The first line contains and ().
Each of the next lines describes a pair of videos that FJ compared manually. Each line contains three integers , , and (, ), indicating that videos and are connected and have relevance .
Each of the next lines describes one of Farmer John’s queries. Each line contains two integers, and (, ), meaning that in FJ’s -th query, when , you should report how many videos would be recommended in the 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号