Spectral::Cup 2026 Round 2 (Codeforces Round 1100, Div. 1 + Div. 2)
I
末端节点
The Endians
Codeforces
3500
原题
← H→

末端节点AI (CI翻译)

You are given a tree of $n$ nodes, where node $i$ has weight $w_i$, and an integer $k$.

给定一棵包含 $n$ 个节点的树,其中节点 $i$ 的权值为 $w_i$,以及一个整数 $k$。AI (CI翻译)

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

将树以节点 $x$ 为根。你可以选择一个节点子集 $S$,满足 $|S| = k$ 且 $x \in S$。令 $f(i)$ 表示从节点 $i$ 到根节点的路径上,$S$ 中所有节点的权值之和。集合 $S$ 的得分定义为 $\sum_{i \in S} f(i)$。AI (CI翻译)

For each node $1 \le x \le n$, find the maximum possible score among all subsets $S$ if the tree is rooted at node $x$.

对于每个节点 $1 \le x \le n$,求将树以节点 $x$ 为根时,所有满足条件的节点子集中可能得到的最大得分。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 500$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 4000$, $1 \le k \le n$).

每个测试用例的第一行包含两个整数 $n$ 和 $k$($2 \le n \le 4000$)。AI (CI翻译)

The second line of each test case contains $n$ integers $w_1, w_2,\ldots, w_n$ ($1 \le w_i \le 10^9$).

每个测试用例的第二行包含 $n$ 个整数 $w_1, w_2, w_n$($1 \le w_i \le 10^9$)。AI (CI翻译)

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

接下来的 $n - 1$ 行中,每行包含两个整数 $u$ 和 $v$($1 \le u, v \le n$),表示节点 $u$ 和节点 $v$ 之间有一条边。保证给定的图是一棵树。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $4000$.

保证所有测试用例的 $n$ 之和不超过 $4000$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, print $n$ integers. For each $x$ from $1$ to $n$, print the maximum possible score among all subsets $S$ if the tree is rooted at node $x$.

对于每个测试用例,输出 $n$ 个整数。对于每个 $x$(从 $1$ 到 $n$),输出将树以节点 $x$ 为根时,所有满足条件的节点子集中可能得到的最大得分。AI (CI翻译)

Note

说明

AI (CI翻译)

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

在第一个测试用例中,树如下所示:AI (CI翻译)

image

imageAI (CI翻译)

For each $1 \le x \le n$, the following is an optimal set $S$:

对于每个 $1 \le x \le n$,以下是一个最优的集合 $S$:AI (CI翻译)

  • $x = 1$: $S = \{1, 2, 5\}$; the score is $(2) + (12 + 2) + (9 + 2) = 27$,
  • $x = 2$: $S = \{2, 4 ,5\}$; the score is $(12) + (6 + 12) + (9 + 6 + 12) = 57$,
  • $x = 3$: $S = \{2, 3, 5\}$; the score is $(12 + 3) + (3) + (9 + 3) = 30$,
  • $x = 4$: $S = \{2, 4, 5\}$; the score is $(12 + 6) + (6) + (9 + 6) = 39$,
  • $x = 5$: $S = \{2, 4 ,5\}$; the score is $(12 + 6 + 9) + (6 + 9) + (9) = 51$,
  • $x = 6$: $S = \{2 ,4, 6\}$; the score is $(12 + 6 + 7) + (6 + 7) + (7) = 45$.
  • $x = 1$:$S = \{1, 2, 5\}$;得分为 $(2) + (12 + 2) + (9 + 2) = 27$;
  • $x = 2$:$S = \{2, 4 ,5\}$;得分为 $(12) + (6 + 12) + (9 + 6 + 12) = 57$;
  • $x = 3$:$S = \{2, 3, 5\}$;得分为 $(12 + 3) + (3) + (9 + 3) = 30$;
  • $x = 4$:$S = \{2, 4, 5\}$;得分为 $(12 + 6) + (6) + (9 + 6) = 39$;
  • $x = 5$:$S = \{2, 4 ,5\}$;得分为 $(12 + 6 + 9) + (6 + 9) + (9) = 51$;
  • $x = 6$:$S = \{2 ,4, 6\}$;得分为 $(12 + 6 + 7) + (6 + 7) + (7) = 45$。
AI (CI翻译)

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

在第二个测试用例中,对于所有 $x$,$S = \{1, 2, 3, 4, 5\}$。AI (CI翻译)

样例

Input #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
Output #1
27 57 30 39 51 45
54311 15311 12511 12451 12415
120 150 134 170 155 122 150 132 162
InputCopy OutputCopy
题目信息
题号
2229I
时间限制
2 s
内存限制
1024 MB
标签
动态规划
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。