CF2229I.The Endians

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree of nn nodes, where node ii has weight wiw_i, and an integer kk.

Let the tree be rooted at node xx. You may select a subset SS of the nodes such that ∣S∣=k|S| = k and x∈Sx \in S. Let f(i)f(i) be the sum of the weights of all nodes in SS on the path from node ii to the root. The score of SS is ∑i∈Sf(i)\sum_{i \in S} f(i).

For each node 1≤x≤n1 \le x \le n, find the maximum possible score among all subsets SS if the tree is rooted at node xx.

给你一棵包含 nn 个节点的树,其中节点 ii 的权值为 wiw_i,以及一个整数 kk。

将树以节点 xx 为根。你可以选择一个节点子集 SS,满足 ∣S∣=k|S| = k 且 x∈Sx \in S。令 f(i)f(i) 表示从节点 ii 到根节点的路径上所有属于 SS 的节点的权值之和。子集 SS 的得分为 ∑i∈Sf(i)\sum_{i \in S} f(i)。

对每个节点 1≤x≤n1 \le x \le n,求当树以节点 xx 为根时,所有满足条件的子集 SS 所能获得的最大得分。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains two integers nn and kk (2≤n≤40002 \le n \le 4000, 1≤k≤n1 \le k \le n).

The second line of each test case contains nn integers w1,w2,…,wnw_1, w_2,\ldots, w_n (1≤wi≤1091 \le w_i \le 10^9).

Each of the next n−1n - 1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n), indicating that nodes uu and vv are connected by an edge. It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 40004000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(2≤n≤40002 \le n \le 4000,1≤k≤n1 \le k \le n)。

每个测试用例的第二行包含 nn 个整数 w1,w2,…,wnw_1, w_2,\ldots, w_n(1≤wi≤1091 \le w_i \le 10^9)。

接下来的 n−1n - 1 行中,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n),表示节点 uu 和 vv 之间存在一条边。保证所给图是一棵树。

保证所有测试用例的 nn 值之和不超过 40004000。

输出格式

For each test case, print nn integers. For each xx from 11 to nn, print the maximum possible score among all subsets SS if the tree is rooted at node xx.

对于每个测试用例,输出 nn 个整数。对于每个从 11 到 nn 的 xx,输出当树以节点 xx 为根时,所有子集 SS 所能获得的最大可能得分。

输入输出样例

  • 输入#1

    3
    6 3
    2 12 3 6 9 7
    1 2
    1 3
    3 4
    4 5
    4 6
    5 5
    10000 1000 100 10 1
    1 2
    2 3
    3 4
    3 5
    9 5
    7 11 5 16 13 10 12 9 15
    8 1
    1 6
    3 7
    4 6
    6 7
    6 5
    9 1
    2 8

    输出#1

    27 57 30 39 51 45 
    54311 15311 12511 12451 12415 
    120 150 134 170 155 122 150 132 162

说明/提示

In the first test case, the tree is as follows:

For each 1≤x≤n1 \le x \le n, the following is an optimal set SS:

  • x=1x = 1: S=1,2,5S = {1, 2, 5}; the score is (2)+(12+2)+(9+2)=27(2) + (12 + 2) + (9 + 2) = 27,
  • x=2x = 2: S=2,4,5S = {2, 4 ,5}; the score is (12)+(6+12)+(9+6+12)=57(12) + (6 + 12) + (9 + 6 + 12) = 57,
  • x=3x = 3: S=2,3,5S = {2, 3, 5}; the score is (12+3)+(3)+(9+3)=30(12 + 3) + (3) + (9 + 3) = 30,
  • x=4x = 4: S=2,4,5S = {2, 4, 5}; the score is (12+6)+(6)+(9+6)=39(12 + 6) + (6) + (9 + 6) = 39,
  • x=5x = 5: S=2,4,5S = {2, 4 ,5}; the score is (12+6+9)+(6+9)+(9)=51(12 + 6 + 9) + (6 + 9) + (9) = 51,
  • x=6x = 6: S=2,4,6S = {2 ,4, 6}; the score is (12+6+7)+(6+7)+(7)=45(12 + 6 + 7) + (6 + 7) + (7) = 45.

In the second test case, S=1,2,3,4,5S = {1, 2, 3, 4, 5} for all xx.

在第一个测试用例中,树的结构如下:

对每个 1≤x≤n1 \le x \le n,以下为一个最优集合 SS:

  • x=1x = 1:S={1,2,5}S = \{1, 2, 5\};得分为 (2)+(12+2)+(9+2)=27(2) + (12 + 2) + (9 + 2) = 27,
  • x=2x = 2:S={2,4,5}S = \{2, 4, 5\};得分为 (12)+(6+12)+(9+6+12)=57(12) + (6 + 12) + (9 + 6 + 12) = 57,
  • x=3x = 3:S={2,3,5}S = \{2, 3, 5\};得分为 (12+3)+(3)+(9+3)=30(12 + 3) + (3) + (9 + 3) = 30,
  • x=4x = 4:S={2,4,5}S = \{2, 4, 5\};得分为 (12+6)+(6)+(9+6)=39(12 + 6) + (6) + (9 + 6) = 39,
  • x=5x = 5:S={2,4,5}S = \{2, 4, 5\};得分为 (12+6+9)+(6+9)+(9)=51(12 + 6 + 9) + (6 + 9) + (9) = 51,
  • x=6x = 6:S={2,4,6}S = \{2, 4, 6\};得分为 (12+6+7)+(6+7)+(7)=45(12 + 6 + 7) + (6 + 7) + (7) = 45。

在第二个测试用例中,对所有 xx,均有 S={1,2,3,4,5}S = \{1, 2, 3, 4, 5\}。

输入解题思路,AI测评打分。不知道怎么写?

首页