CF2209F.Dynamic Values And Maximum Sum

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a tree∗^{\text{∗}} with nn vertices numbered from 1 to nn, where each vertex ii has an initial value aia_i. You perform kk operations. The total is 00 initially. In each operation:

  1. Choose a vertex rr and root the tree at rr.
  2. Add the current value of rr to the total and set the value of rr to 00.
  3. For each vertex $ u $ that is not a leaf†^{\text{†}}, find the leaves in the subtree‡^{\text{‡}} of uu that are at the maximum distance from uu. Among them, select the one with the smallest index, denoted as the destination for uu. Add the current value of uu to the destination and set the value of uu to 00.

Find the maximum possible total.

∗^{\text{∗}}A tree is a connected graph without cycles.

†^{\text{†}}A leaf is any vertex without children.

‡^{\text{‡}}A subtree of vertex vv is the subgraph of vv, all its descendants, and all the edges between them.

给你一棵有 nn 个顶点的树∗^{\text{∗}},顶点编号为 11 到 nn,其中每个顶点 ii 有一个初始值 aia_i。你需要执行 kk 次操作。初始时总和为 00。每次操作包含以下步骤:

  1. 选择一个顶点 rr,并将树以 rr 为根重新定向(即重设根节点)。
  2. 将 rr 的当前值加到总和中,并将 rr 的值置为 00。
  3. 对于每个非叶子顶点†^{\text{†}} uu,找出其子树‡^{\text{‡}} 中距离 uu 最远的所有叶子顶点;在这些叶子顶点中,选取编号最小者,记作 uu 的目标顶点。将 uu 的当前值加到该目标顶点上,并将 uu 的值置为 00。

求最终总和的最大可能值。

∗^{\text{∗}} 树是无环的连通图。

†^{\text{†}} 叶子顶点指没有子节点的顶点。

‡^{\text{‡}} 顶点 vv 的子树是指由 vv、其所有后代顶点以及它们之间的所有边构成的子图。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10 ^ 4). The description of the test cases follows.

The first line of each test case contains two integers nn and kk (1≤k≤n≤3⋅1051 \le k \le n \le 3 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the initial values of vertices.

Each of the following n−1n-1 lines contains two integers uu and vv, denoting an edge connecting vertex uu and vv. It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053\cdot 10 ^ 5.

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

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k≤n≤3⋅1051 \le k \le n \le 3 \cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9)——顶点的初始值。

接下来的 n−1n-1 行中,每行包含两个整数 uu 和 vv,表示一条连接顶点 uu 和 vv 的边。保证所给边构成一棵树。

保证所有测试用例的 nn 值之和不超过 3⋅1053\cdot 10 ^ 5。

输出格式

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:

  1. Choose vertex 11 as the root. Add a1=19a_1 = 19 to the total and set a1=0a_1 = 0.

    After rooting the tree at 11, for every non-leaf vertex uu, move its value to the leaf in its subtree that is farthest from uu (breaking ties by smallest index). Under this rooting:

    • The value of vertex 22 is transferred to vertex 44, so a2a_2 becomes 00 and a4a_4 becomes 101101.
    • The values of vertices 33, 44, and 55 remain at their respective positions according to the rule.
  2. Choose vertex 33 as the root. Add a3=39a_3 = 39 to the total and set a3=0a_3 = 0.

    After redistribution under this rooting, the values of vertices 44 and 55 remain at their positions.

  3. Choose vertex 44 as the root. Add a4=101a_4 = 101 to the total and set a4=0a_4 = 0.

    After redistribution, the value at vertex 55 remains unchanged.

  4. Choose vertex 55 as the root. Add a5=2a_5 = 2 to the total and set a5=0a_5 = 0.

The total obtained is 19+39+101+2=16119 + 39 + 101 + 2 = 161.

In the sixth test case, choose vertex 22 as the root. Add its value to the total, obtaining 1 000 000 0001\,000\,000\,000.

在第一个测试用例中:

  1. 选择顶点 11 作为根。将 a1=19a_1 = 19 加入总和,并令 a1=0a_1 = 0。

    将树以 11 为根重新定根后,对每个非叶子顶点 uu,将其值转移到其子树中距离 uu 最远的叶子顶点(若存在多个最远叶子,则选择下标最小者)。在此定根方式下:

    • 顶点 22 的值被转移到顶点 44,因此 a2a_2 变为 00,而 a4a_4 变为 101101。
    • 顶点 33、44 和 55 的值根据规则保持在其各自位置不变。
  2. 选择顶点 33 作为根。将 a3=39a_3 = 39 加入总和,并令 a3=0a_3 = 0。

    在此定根方式下重新分配后,顶点 44 和 55 的值仍保留在其各自位置。

  3. 选择顶点 44 作为根。将 a4=101a_4 = 101 加入总和,并令 a4=0a_4 = 0。

    重新分配后,顶点 55 处的值保持不变。

  4. 选择顶点 55 作为根。将 a5=2a_5 = 2 加入总和,并令 a5=0a_5 = 0。

所得总和为 19+39+101+2=16119 + 39 + 101 + 2 = 161。

在第六个测试用例中,选择顶点 22 作为根。将其值加入总和,得到 1 000 000 0001\,000\,000\,000。

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

首页