This page is still under construction.

Parts of this page are still being built. What you see may change.

MooTube (Gold)

Time limit2sMemory limit512 MB

Summary
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 NN videos, 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, 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 N−1N-1 pairs of videos and computed the USADO of each pair himself. He chose those N−1N-1 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 KK, then recommends every video whose USADO with that video is at least KK. Too many recommendations would keep the cows from working, so he wants to choose KK well. Given KK and a video number, report how many videos are recommended.

Input

The first line contains the number of videos NN and the number of questions QQ. (1≤N≤100 0001 \le N \le 100\,000, 1≤Q≤100 0001 \le Q \le 100\,000)

Each of the next N−1N-1 lines contains one measurement as three integers pip_i, qiq_i, rir_i (1≤pi,qi≤N1 \le p_i, q_i \le N, 1≤ri≤1 000 000 0001 \le r_i \le 1\,000\,000\,000), meaning that video pip_i and video qiq_i are connected with USADO rir_i.

Each of the next QQ lines contains one question as two integers kik_i and viv_i (1≤ki≤1 000 000 0001 \le k_i \le 1\,000\,000\,000, 1≤vi≤N1 \le v_i \le N), asking how many videos are recommended to a cow watching video viv_i when K=kiK = k_i.

Output

Print QQ lines. Line ii contains the answer to the ii-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 min⁡(3,2)=2\min(3, 2) = 2, the USADO of videos 1 and 4 is min⁡(3,4)=3\min(3, 4) = 3, and the USADO of videos 3 and 4 is 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.

Examples1

  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