Under construction

Still being built. Things may change

Deliveries

Interview

Time limit2sMemory limit512 MB

Summary
Given a tree of warehouses with road lengths, find the fewest stops on the path from S to F when the truck can drive at most T km per charge.
Level

Medium6 of 10

Topics
Tree, Greedy, Binary search
Solved
No attempts yet

Problem

Sam is working as a truck driver and frequently delivers goods between various warehouses. There are N warehouses in total. Some warehouses are connected, and there is exactly one way to get from each warehouse to every other warehouse, possibly passing through several other warehouses along the way.

To make his trips, Sam uses various battery-powered trucks. Each truck has a folding solar panel that charges the battery, but only while the truck is stopped. While traveling from one warehouse to another, Sam can stop at any point and wait to recharge the battery. When the battery runs out of charge, he cannot move. Sam must also stop at every warehouse he passes through for security reasons. When he stops at a warehouse, he can also recharge the battery.

Sam does not like to stop too often, so he asks you to find the smallest number of stops for each route he takes. This includes both stops made for recharging and stops at warehouses, including the starting and final warehouses (for loading and unloading).

Input

The first line contains two integers N and Q, the number of warehouses and the number of routes Sam takes, where 1≤N,Q≤100,0001 \le N, Q \le 100{,}000. The next N−1N - 1 lines describe the roads between warehouses. Each line contains three integers U, V, and D. U and V are indices of warehouses, where 1≤U,V≤N1 \le U, V \le N. D is the length of the road between warehouses U and V in kilometres, where 1≤D≤20,0001 \le D \le 20{,}000. The next Q lines describe the routes Sam takes. Each line contains three integers S, F, and T. S and F are the indices of the starting and final warehouses on the route, where 1≤S,F≤N1 \le S, F \le N and S≠FS \neq F. T is the capacity of the battery of the truck used for this route, expressed as the distance the truck can travel on a full charge (in kilometres), where 1≤T≤20,0001 \le T \le 20{,}000.

Output

For each of the Q routes, output one line containing the smallest number of stops that need to be made along the route.

Examples2

  1. Example 1

    Input
    7 5
    1 2 1
    2 3 2
    2 4 3
    4 5 4
    4 6 5
    4 7 6
    3 7 2
    2 6 1
    5 7 3
    1 4 1
    3 7 1
    
    Expected output
    7
    9
    5
    5
    12
    
  2. Example 2

    Input
    4 3
    1 2 5
    2 3 10
    3 4 20
    1 4 20
    1 4 10
    1 4 5
    
    Expected output
    4
    5
    8