Under construction

Still being built. Things may change

MooTube (Silver)

Interview

Time limit2sMemory limit512 MB

Summary
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 NN videos (1≤N≤50001 \le N \le 5000), numbered 1 through NN. 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 N−1N-1 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 NN 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 KK, then recommends every video whose USADO with that video is at least KK. He worries that too many recommendations will keep the cows from working, so he wants to settle on a sensible KK. For each of his questions, report how many videos are recommended.

Input

The first line contains NN and QQ (1≤Q≤50001 \le Q \le 5000).

Each of the next N−1N-1 lines holds one pair John measured himself, given as three integers pip_i, qiq_i, rir_i (1≤pi,qi≤N1 \le p_i, q_i \le N, 1≤ri≤1091 \le r_i \le 10^9). Video pip_i and video qiq_i are linked with USADO rir_i.

Each of the next QQ lines holds one of John's questions, given as two integers kik_i and viv_i (1≤ki≤1091 \le k_i \le 10^9, 1≤vi≤N1 \le v_i \le N). The question asks how many videos are recommended to a cow watching video viv_i when K=kiK = k_i.

Output

Print QQ lines. Line ii holds the answer to John's ii-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 min⁡(3,2)=2\min(3, 2) = 2, videos 1 and 4 have USADO min⁡(3,4)=3\min(3, 4) = 3, and videos 3 and 4 have USADO min⁡(2,4)=2\min(2, 4) = 2.

With K=1K = 1 and video 2, the recommended videos are 1, 3 and 4. With K=4K = 4 and video 1, nothing is recommended. With K=3K = 3 and video 1, videos 2 and 4 are recommended.

Examples2

  1. Example 1

    Input
    4 3
    1 2 3
    2 3 2
    2 4 4
    1 2
    4 1
    3 1
    
    Expected output
    3
    0
    2
    
  2. Example 2

    Input
    5 5
    1 2 7
    2 3 3
    3 4 9
    4 5 5
    1 1
    4 3
    5 3
    8 4
    10 5
    
    Expected output
    4
    2
    2
    1
    0