#P10805. [CEOI 2024] 加油站

[CEOI 2024] 加油站

Problem Description

This problem is translated from CEOI 2024 Day 2 T2「Petrol stations」.

The Czech highway network consists of NN cities and N1N-1 roads, and the length (in kilometers) of each road is known. We know that there is exactly one path between any two cities. Also, each city has exactly one petrol station, and there are no petrol stations anywhere else.

One day, many people decide to travel by car. In total, there are N2N^2 cars on the roads. Strangely, for every ordered pair of cities (a,b)(a, b), there is exactly one car traveling from city aa to city bb, driving along the unique path between these two cities. Since all Czech people drive Škoda cars, every car has a fuel tank capacity of KK liters and consumes one liter of fuel per kilometer. Before departure, each car’s tank is full. Also, Czech people are very regular. Due to laziness, they refuel only when they do not have enough fuel to reach the next city (they are allowed to enter a city with an empty tank). Once they are forced to stop at a petrol station, they always fill the tank completely.

The Czech tax authority wants to know how many cars stopped at each petrol station on that day. Given this predictable behavior, you should be able to compute it easily.

Input Format

The first line contains two space-separated integers NN and KK, representing the number of cities and the fuel tank capacity of each car.

The next N1N-1 lines describe the roads. Each line contains three space-separated integers ui,vi,liu_i, v_i, l_i, where uiu_i and viv_i are the city indices connected by the ii-th road, and lil_i is the length (in kilometers) of this road. The cities are numbered from 00 to N1N-1. It is guaranteed that there is exactly one path between any two cities.

Output Format

Output NN lines. The ii-th line should be the number of cars that stopped at the petrol station in city ii, for cities 00 to N1N-1.

3 1
0 1 1
1 2 1
0
2
0
6 2
0 1 1
1 2 1
2 3 1
3 4 2
4 5 1
0
3
3
12
8
0

Hint

Sample Explanation 1.

There are 33 cities arranged in a straight line. The roads connecting them have length 11, and the tank capacity is 11 liter. Only the cars traveling between the two outer cities will refuel in the middle city.

Sample Explanation 2.

There are 66 cities arranged in a straight line, and the tank capacity is 22 liters. Many cars need to refuel in city 33 and city 44. This makes sense, because the road between city 33 and city 44 has length 22 kilometers.

For all input data, the following constraints hold:

  • 2N700002 \leq N \leq 70\,000
  • 1K1091 \leq K \leq 10^9
  • 0liK (0iN2)0 \leq l_i \leq K\ (0 \leq i \leq N-2)

Let DD be the maximum number of roads connected to a single city. The detailed additional constraints and scores for subtasks are listed in the table below.

Subtask Additional Constraints Score
11 N1000,K1000N \leq 1\,000, K \leq 1\,000 1818
22 D2D \le 2 and li=1 (0iN2)l_i = 1\ (0 \leq i \leq N-2) 88
33 D2D \le 2 1010
44 K10,D10K \leq 10, D \leq 10 1212
55 K10K \leq 10 1717
66 No additional constraints. 3535

Translated by ChatGPT 5