CF2192D.Cost of Tree

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

For a tree TT with root rr, where each node uu has a value aua_u associated with it, the cost of the tree defined as:

sum_uinT(a_ucdotd(r,u))\\sum\_{u\\in T} (a\_u \\cdot d(r,u))

Here, this sum is taken over all nodes uu in the tree TT, and d(r,u)d(r,u) denotes the number of edges on the shortest path from node rr to node uu on a tree.

You are given a tree consisting of nn nodes, rooted at node 11. Each node ii has a value aia_i assigned to it. For each rr from 11 to nn, please solve the following problem independently:

Consider the subtree of node rr with respect to node 11. Formally, the subtree of node rr is the tree consisting of all nodes uu such that the shortest path from 11 to uu contains rr.

Find the maximum cost of the subtree after performing at most one operation of the following type on the subtree:

  • Choose any node uu (u≠ru \neq r). Remove the edge from uu to the parent of node uu∗^{\text{∗}}. Then, add an edge from uu to any node vv that is still reachable from rr. It can be shown that after this operation, the graph remains a tree.

As an example, below shows an example of an operation with r=1r=1,u=5u=5, and v=4v=4.

∗^{\text{∗}}Formally, remove the edge from uu to pp, where pp is the unique node satisfying d(u,p)=1d(u,p)=1 and d(u,r)=d(p,r)+1d(u,r)=d(p,r)+1

对于一棵以节点 rr 为根的树 TT,其中每个节点 uu 关联一个值 aua_u,该树的代价定义为:

∑u∈T(au⋅d(r,u))\sum_{u\in T} (a_u \cdot d(r,u))

此处,求和遍历树 TT 中的所有节点 uu,而 d(r,u)d(r,u) 表示树上从节点 rr 到节点 uu 的最短路径所包含的边数。

给定一棵含 nn 个节点的树,其根节点为节点 11。每个节点 ii 被赋予一个值 aia_i。对每个 rr(从 11 到 nn),请独立求解如下问题:

考虑以节点 11 为参考时节点 rr 的子树。形式化地,节点 rr 的子树是指所有满足“从节点 11 到节点 uu 的最短路径经过 rr”的节点 uu 所构成的树。

在对该子树至多执行一次如下操作的前提下,求该子树所能达到的最大代价:

  • 任选一个节点 uu(要求 u≠ru \neq r)。删除节点 uu 与其父节点之间的边∗^{\text{∗}};然后,将节点 uu 与任意一个仍能从 rr 到达的节点 vv 连接一条新边。可以证明,执行该操作后图仍为一棵树。

例如,下图展示了一个操作示例,其中 r=1r=1、u=5u=5、v=4v=4:

∗^{\text{∗}}形式化地,删除节点 uu 与节点 pp 之间的边,其中 pp 是唯一满足 d(u,p)=1d(u,p)=1 且 d(u,r)=d(p,r)+1d(u,r)=d(p,r)+1 的节点。

输入格式

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 testcase contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the count of nodes in the tree.

The second line of each testcase contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤2⋅1051 \le a_i \le 2 \cdot 10^5).

Then n−1n − 1 lines follow, the ii-th line containing two integers uu and vv (1≤u,v≤n1 \le u, v \le n) — the two nodes that the ii-th edge connects.

It is guaranteed that the given edges form a tree.

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

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 树中节点的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤2⋅1051 \le a_i \le 2 \cdot 10^5)。

接下来是 n−1n - 1 行,其中第 ii 行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n)—— 表示第 ii 条边所连接的两个节点。

保证给定的边构成一棵树。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print nn numbers – the answers for r=1,2,…,nr=1,2,\ldots,n.

对于每个测试用例,输出 nn 个数字——即 r=1,2,…,nr=1,2,\ldots,n 对应的答案。

输入输出样例

  • 输入#1

    3
    5
    1 3 2 1 2
    1 2
    2 3
    3 4
    3 5
    7
    1 2 3 1 3 2 1
    1 2
    2 3
    3 4
    4 5
    4 6
    3 7
    5
    5 4 3 2 1
    1 2
    2 3
    3 4
    4 5

    输出#1

    18 10 5 0 0 
    40 28 18 8 0 0 0 
    20 10 4 1 0

说明/提示

In the first test case, for r=1r=1, it is optimal to choose u=5u=5 and v=4v=4. The cost of the tree is then 1⋅0+3⋅1+2⋅2+1⋅3+2⋅4=181\cdot 0 + 3\cdot 1+2\cdot 2+1\cdot 3+2\cdot 4=18. It can be shown that a larger cost cannot be obtained over all legal operations.

For r=4r=4 for example, there is only 11 node in the subtree, so there is no operation possible. The only possible cost of the subtree is 00.

在第一个测试用例中,当 r=1r=1 时,选择 u=5u=5 和 v=4v=4 是最优的。此时树的代价为 1⋅0+3⋅1+2⋅2+1⋅3+2⋅4=181\cdot 0 + 3\cdot 1+2\cdot 2+1\cdot 3+2\cdot 4=18。可以证明,在所有合法操作下,无法得到更大的代价。

例如,当 r=4r=4 时,子树中仅包含 11 个节点,因此无法执行任何操作。该子树的唯一可能代价为 00。

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

首页