Petrol stations
Time limit3.5sMemory limit2048 MB
Given a tree of N cities and a tank capacity K, count how many of the N^2 cars stop at each city's petrol station under the refuel-when-needed rule.
- Level
Hard8 of 10
- Topics
- Tree, Divide and conquer, Sorting
- Solved
- No attempts yet
Problem
The Czech highway network consists of cities and roads with known lengths in kilometers. There is exactly one path between each pair of cities. Each city has one petrol station, and there are no other petrol stations.
One day, several people went on a car trip. A total of cars traveled. For each ordered pair of cities , exactly one car drove from city to city along the only path between them. Every car has the same fuel tank capacity of liters. A car consumes one liter of petrol per kilometer traveled. Before departure, each tank is full.
The Czechs are predictable and lazy. A car refuels only when it does not have enough petrol to reach the next city. It can still enter a city with an empty tank. When a car is forced to stop at a petrol station, it fills the tank completely.
The Czech tax authority wants to know how many cars stopped at each petrol station during the day. Using this predictable behavior, the count can be computed easily.
Input
The first line contains two space-separated integers and , the number of cities and the fuel tank capacity of each car. The next lines describe the roads. Each line contains three space-separated integers , , and , where and are the cities connected by the -th road and is its length in kilometers. Cities are numbered from to . It is guaranteed that there is exactly one path between every pair of cities.
Output
Output lines. The -th line (starting from city ) contains the number of cars that stopped at the petrol station in city .
Constraints
- (for each such that )