MooTube (Silver)
InterviewTime limit2sMemory limit512 MB
Given a weighted tree, for each query (k, v) count the vertices whose bottleneck distance (minimum edge on the path) from v is at least k.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Union-find
- Solved
- No attempts yet
Problem
Farmer John built a video sharing service called MooTube in his spare time. On MooTube his cows share funny videos with each other. The cows have already uploaded videos (), numbered 1 through . John still has no good way to help a cow find a new video she would like.
John decided to build a list of related videos for every video on MooTube. A cow then gets recommendations that are close to the video she is watching.
To measure how close two videos are, John invented a unit called USADO. He picked pairs of videos and computed the USADO of each pair himself. Treat each video as a node and each chosen pair as a link. The videos then form one network in which exactly one path runs between any two videos. John defined the USADO of two videos as the smallest USADO among the links on that path.
John picks a video and a value , then recommends every video whose USADO with that video is at least . He worries that too many recommendations will keep the cows from working, so he wants to settle on a sensible . For each of his questions, report how many videos are recommended.
Input
The first line contains and ().
Each of the next lines holds one pair John measured himself, given as three integers , , (, ). Video and video are linked with USADO .
Each of the next lines holds one of John's questions, given as two integers and (, ). The question asks how many videos are recommended to a cow watching video when .
Output
Print lines. Line holds the answer to John's -th question.
Notes
Take the first example. Videos 1 and 2 have USADO 3, videos 2 and 3 have USADO 2, and videos 2 and 4 have USADO 4. So videos 1 and 3 have USADO , videos 1 and 4 have USADO , and videos 3 and 4 have USADO .
With and video 2, the recommended videos are 1, 3 and 4. With and video 1, nothing is recommended. With and video 1, videos 2 and 4 are recommended.