MooTube (Gold)
Time limit2sMemory limit512 MB
On a weighted tree, USADO between two videos is the minimum edge weight on their path. For each query (K, v), count vertices whose USADO with v is at least K.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Sorting, DFS
- 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, so that a cow is recommended videos close to the one she is watching.
How close two videos are is measured by USADO, a unit John invented. He picked pairs of videos and computed the USADO of each pair himself. He chose those pairs so that exactly one path runs from any video to any other video. Treating the videos as vertices and the measured pairs as edges, the whole thing is one tree. The USADO of two videos he did not measure directly is the smallest USADO among the edges on the path between them.
John picks one video and a value , then recommends every video whose USADO with that video is at least . Too many recommendations would keep the cows from working, so he wants to choose well. Given and a video number, report how many videos are recommended.
Input
The first line contains the number of videos and the number of questions . (, )
Each of the next lines contains one measurement as three integers , , (, ), meaning that video and video are connected with USADO .
Each of the next lines contains one question as two integers and (, ), asking how many videos are recommended to a cow watching video when .
Output
Print lines. Line contains the answer to the -th question.
Hint
In the sample the USADO of videos 1 and 2 is 3, the USADO of videos 2 and 3 is 2, and the USADO of videos 2 and 4 is 4. From those measurements the USADO of videos 1 and 3 is , the USADO of videos 1 and 4 is , and the USADO of videos 3 and 4 is .
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.