#P7600. [APIO2021] 封闭道路

    ID: 6603 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021APIO交互题Special JudgeO2优化

[APIO2021] 封闭道路

Description

In Si Shui City, there are NN intersections (numbered from 00 to N1N-1). These intersections are connected by N1N-1 two-way roads (numbered from 00 to N2N-2), so that between any pair of intersections there is a unique path using these roads. Road ii (0iN20 \le i \le N-2) connects intersections U[i]U[i] and V[i]V[i].

To raise awareness about environmental protection, the mayor of Si Shui City, Pak Dengklek, plans to hold a Car-Free Day. To promote this event, Pak Dengklek will organize road closures. Pak Dengklek will first choose a non-negative integer kk, and then close some roads so that each intersection is directly connected to at most kk roads that remain open. The cost to close road ii is W[i]W[i].

Please help Pak Dengklek compute, for every possible non-negative integer kk (0kN10 \le k \le N-1), the minimum total cost to close roads.

You need to implement the following function:

int64[] minimum_closure_costs(int N, int[] U, int[] V, int[] W)

  • NN: the number of intersections in Si Shui City.

  • UU and VV: arrays of size N1N-1, where intersection U[i]U[i] and intersection V[i]V[i] are directly connected by road ii.

  • WW: an array of size N1N-1, where the cost to close road ii is W[i]W[i].

  • The function must return an array of size NN. For each kk (0kN10 \le k \le N-1), element kk is the minimum total cost such that each intersection is directly connected to at most kk open roads.

This function will be called exactly once.

Input Format

The sample grader reads the input data in the following format:

  • Line 11: NN
  • Line 2+i2+i (0iN20 \le i \le N-2): U[i]U[i] V[i]V[i] W[i]W[i]

Output Format

The sample grader outputs only one line containing an array, representing the return value of minimum_closure_costs.

5
0 1 1
0 2 4
0 3 3
2 4 2

10 5 1 0 0

4
0 1 5
2 0 10
0 3 5

20 10 5 0

Hint

Examples

Example 11

Consider the following call:

minimum_closure_costs(5, [0, 0, 0, 2], [1, 2, 3, 4], [1, 4, 3, 2])

In this example, there are 55 intersections and 44 roads, connecting intersections (0,1),(0,2),(0,3)(0,1),(0,2),(0,3), and (2,4)(2,4). The costs to close them are 1,4,31,4,3, and 22, respectively.

To achieve the minimum total cost:

  • If Pak Dengklek chooses k=0k=0, then all roads must be closed, and the total cost is 1+4+3+2=101+4+3+2=10.
  • If Pak Dengklek chooses k=1k=1, then roads 00 and 11 must be closed, and the total cost is 1+4=51+4=5.
  • If Pak Dengklek chooses k=2k=2, then road 00 must be closed, and the total cost is 11.
  • If Pak Dengklek chooses k=3k=3 or k=4k=4, then no roads need to be closed.

Therefore, minimum_closure_costs should return the array [10,5,1,0,0][10,5,1,0,0].

Example 22

Consider the following call:

minimum_closure_costs(4, [0, 2, 0], [1, 0, 3], [5, 10, 5])

In this example, there are 44 intersections and 33 roads, connecting intersections (0,1),(2,0)(0,1),(2,0), and (0,3)(0,3). The costs to close them are 5,105,10, and 55, respectively.

To achieve the minimum total cost:

  • If Pak Dengklek chooses k=0k=0, then all roads must be closed, and the total cost is 5+10+5=205+10+5=20.
  • If Pak Dengklek chooses k=1k=1, then roads 00 and 22 must be closed, and the total cost is 5+5=105+5=10.
  • If Pak Dengklek chooses k=2k=2, then either road 00 or road 22 must be closed, and the total cost is 55.
  • If Pak Dengklek chooses k=3k=3, then no roads need to be closed.

Therefore, minimum_closure_costs should return the array [20,10,5,0][20,10,5,0].

Constraints

  • 2N1052 \le N \le 10^5
  • 0U[i],V[i]N10 \le U[i],V[i] \le N-1 (0iN2)(0 \le i \le N-2)
  • Any pair of intersections can reach each other via the roads.
  • 1W[i]1091 \le W[i] \le 10^9 (0iN2)(0 \le i \le N-2).

Hint

Translated by ChatGPT 5