CF2209F.Dynamic Values And Maximum Sum
省选/NOI-
通过率:0%
时间限制:5.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree∗ with n vertices numbered from 1 to n, where each vertex i has an initial value ai. You perform k operations. The total is 0 initially. In each operation:
- Choose a vertex r and root the tree at r.
- Add the current value of r to the total and set the value of r to 0.
- For each vertex $ u $ that is not a leaf†, find the leaves in the subtree‡ of u that are at the maximum distance from u. Among them, select the one with the smallest index, denoted as the destination for u. Add the current value of u to the destination and set the value of u to 0.
Find the maximum possible total.
∗A tree is a connected graph without cycles.
†A leaf is any vertex without children.
‡A subtree of vertex v is the subgraph of v, all its descendants, and all the edges between them.
给你一棵有 n 个顶点的树∗,顶点编号为 1 到 n,其中每个顶点 i 有一个初始值 ai。你需要执行 k 次操作。初始时总和为 0。每次操作包含以下步骤:
- 选择一个顶点 r,并将树以 r 为根重新定向(即重设根节点)。
- 将 r 的当前值加到总和中,并将 r 的值置为 0。
- 对于每个非叶子顶点† u,找出其子树‡ 中距离 u 最远的所有叶子顶点;在这些叶子顶点中,选取编号最小者,记作 u 的目标顶点。将 u 的当前值加到该目标顶点上,并将 u 的值置为 0。
求最终总和的最大可能值。
∗ 树是无环的连通图。
† 叶子顶点指没有子节点的顶点。
‡ 顶点 v 的子树是指由 v、其所有后代顶点以及它们之间的所有边构成的子图。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and k (1≤k≤n≤3⋅105).
The second line contains n integers a1,a2,…,an (1≤ai≤109) — the initial values of vertices.
Each of the following n−1 lines contains two integers u and v, denoting an edge connecting vertex u and v. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 k(1≤k≤n≤3⋅105)。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)——顶点的初始值。
接下来的 n−1 行中,每行包含两个整数 u 和 v,表示一条连接顶点 u 和 v 的边。保证所给边构成一棵树。
保证所有测试用例的 n 值之和不超过 3⋅105。
输出格式
For each test case, output an integer — the maximum possible total.
对于每个测试用例,输出一个整数——可能的最大总和。
输入输出样例
输入#1
7 5 4 19 20 39 81 2 1 2 1 3 2 4 2 5 5 3 12 21 39 8 21 1 2 1 3 3 4 2 5 5 2 129 216 32 83 221 1 2 1 3 3 4 4 5 5 3 15 15 15 15 15 1 2 1 3 1 4 1 5 1 1 1 2 1 1 1000000000 1 2 7 2 8 3 5 7 9 1 6 4 3 7 5 5 2 2 3 3 6 6 1
输出#1
161 101 681 60 1 1000000000 32
说明/提示
In the first test case:
-
Choose vertex 1 as the root. Add a1=19 to the total and set a1=0.
After rooting the tree at 1, for every non-leaf vertex u, move its value to the leaf in its subtree that is farthest from u (breaking ties by smallest index). Under this rooting:
- The value of vertex 2 is transferred to vertex 4, so a2 becomes 0 and a4 becomes 101.
- The values of vertices 3, 4, and 5 remain at their respective positions according to the rule.
-
Choose vertex 3 as the root. Add a3=39 to the total and set a3=0.
After redistribution under this rooting, the values of vertices 4 and 5 remain at their positions.
-
Choose vertex 4 as the root. Add a4=101 to the total and set a4=0.
After redistribution, the value at vertex 5 remains unchanged.
-
Choose vertex 5 as the root. Add a5=2 to the total and set a5=0.
The total obtained is 19+39+101+2=161.
In the sixth test case, choose vertex 2 as the root. Add its value to the total, obtaining 1000000000.
在第一个测试用例中:
-
选择顶点 1 作为根。将 a1=19 加入总和,并令 a1=0。
将树以 1 为根重新定根后,对每个非叶子顶点 u,将其值转移到其子树中距离 u 最远的叶子顶点(若存在多个最远叶子,则选择下标最小者)。在此定根方式下:
- 顶点 2 的值被转移到顶点 4,因此 a2 变为 0,而 a4 变为 101。
- 顶点 3、4 和 5 的值根据规则保持在其各自位置不变。
-
选择顶点 3 作为根。将 a3=39 加入总和,并令 a3=0。
在此定根方式下重新分配后,顶点 4 和 5 的值仍保留在其各自位置。
-
选择顶点 4 作为根。将 a4=101 加入总和,并令 a4=0。
重新分配后,顶点 5 处的值保持不变。
-
选择顶点 5 作为根。将 a5=2 加入总和,并令 a5=0。
所得总和为 19+39+101+2=161。
在第六个测试用例中,选择顶点 2 作为根。将其值加入总和,得到 1000000000。
输入解题思路,AI测评打分。不知道怎么写?