#P17019. [ROI 2026 Day1] 泰莫利亚的调查

[ROI 2026 Day1] 泰莫利亚的调查

Problem Description

Temeria is one of the most powerful kingdoms in the north, and its capital is the city of Wyzima. The sorceress Triss lives in Wyzima. She has sensed a strong magical anomaly and decides to investigate the Kingdom of Temeria to find the source of the anomaly.

Temeria has nn cities, numbered from 11 to nn, where the capital Wyzima is numbered 11. The cities are connected by n−1n - 1 bidirectional roads. The ii-th road connects cities uiu_i and viv_i and has length wiw_i. It is guaranteed that Triss can travel from any city to any other city using only these roads.

Triss plans to set out from Wyzima, eventually return to Wyzima, and visit all nn cities along the way. She can walk along roads, but it is slow. She has kk teleportation crystals, which can be used to instantly move between cities.

At any time, Triss may leave a crystal in the city she is currently in. Later, she may use a previously left crystal to instantly return to the city where she left that crystal, along the shortest path. After being used, the crystal will shatter. Triss may leave and use crystals in any order. Unfortunately, teleportation does not leave no trace. Specifically, if Triss uses a crystal in city aa and arrives at city bb, then all cities on the shortest path from aa to bb (including aa and bb) will leave magical traces, and all later teleportation routes may no longer pass through these cities.

Please help Triss. For each jj (from 11 to kk, inclusive), find the minimum total distance she needs to walk in order to visit all cities in the kingdom and return to Wyzima, using at most jj crystals.

Input Format

The first line contains two integers nn and kk (2≤n≤500 0002 \le n \le 500\,000; 1≤k≤n1 \le k \le n), representing the number of cities and the number of teleportation crystals Triss has.

The next n−1n - 1 lines describe the roads. Each line contains three integers uiu_i, viv_i, and wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n; 1≤wi≤1091 \le w_i \le 10^9), representing the two cities connected by the ii-th road and its length.

Output Format

Output kk integers. The jj-th integer denotes the minimum total distance Triss needs to walk to visit all cities and return to Wyzima, under the condition that she uses at most jj crystals.

5 1
1 2 1
1 3 1
3 4 1
3 5 1
6
10 2
1 2 10
2 3 6
3 4 8
4 6 5
6 10 7
4 8 6
3 7 6
1 5 4
1 9 9
86
85

Hint

Explanation

In the first sample, Triss’s optimal route is as follows:

  • Triss leaves a crystal in city 11, then walks along the route 1→2→1→3→4→3→51 \to 2 \to 1 \to 3 \to 4 \to 3 \to 5, and then uses the crystal to instantly return to city 11.

In the second sample, the optimal route is as follows:

  • Triss walks along the route 1→5→11 \to 5 \to 1, then leaves a crystal in city 11, then walks along the route $1 \to 9 \to 1 \to 2 \to 3 \to 7 \to 3 \to 4 \to 8 \to 4 \to 6 \to 10$, and uses the crystal in city 11. The length of this route is 8686, and Triss uses exactly one crystal.

In another plan, Triss needs to use two crystals, denoted xx and yy:

  • Triss leaves crystal xx in city 11;
  • Then she walks along the route $1 \to 5 \to 1 \to 9 \to 1 \to 2 \to 3 \to 7 \to 3 \to 4 \to 6$;
  • She leaves crystal yy in city 66;
  • Then she walks from 66 to 1010, and uses crystal yy to return to city 66;
  • Then she walks along the route 6→4→86 \to 4 \to 8;
  • Finally, she uses crystal xx to end the trip.

Subtasks

Subtask Score nn, kk Additional Constraints Dependent Subtasks
1 9 n≤150 000n \le 150\,000;k=1k = 1
2 5 n≤100n \le 100
3 10 n≤5 000n \le 5\,000 2
4 9 n≤150 000n \le 150\,000;k≤300k \le 300 1 – 2
5 11 n≤150 000n \le 150\,000 Complete binary tree∗^{*}, wi=1w_i = 1
6 wi=1w_i = 1 5
7 15 Special graph∗∗^{**}
8 12 Each city has at most 1010 roads
9 11 1 – 8
10 4 n≤300 000n \le 300\,000 1 – 9
11 3 n≤500 000n \le 500\,000 1 – 10

∗^{*} The complete binary tree in subtask 5 refers to a tree consisting of 2s−12^s - 1 vertices (n=2s−1n = 2^s - 1), where for each ii (1≤i≤2s−1−11 \le i \le 2^{s-1} - 1), there are edges (i,2i)(i, 2i) and (i,2i+1)(i, 2i + 1).

∗∗^{**} The special graph in subtask 7 refers to a tree consisting of an odd number nn of vertices, where for each ii (1≤i≤n−121 \le i \le \frac{n-1}{2}), there are edges (1,2i)(1, 2i) and (2i,2i+1)(2i, 2i + 1).

:::align{center} :::

Translated by ChatGPT 5