CF1466D.13th Labour of Heracles

普及/提高-

通过率:0%

时间限制:2.50s

内存限制:256MB

AC君温馨提醒

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

题目描述

You've probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, so on average, a year to accomplish every one of them. As time flows faster these days, you have minutes rather than months to solve this task. But will you manage?

In this problem, you are given a tree with nn weighted vertices. A tree is a connected graph with n−1n - 1 edges.

Let us define its kk-coloring as an assignment of kk colors to the edges so that each edge has exactly one color assigned to it. Note that you don't have to use all kk colors.

A subgraph of color xx consists of these edges from the original tree, which are assigned color xx, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree 00 in such a subgraph.

The value of a connected component is the sum of weights of its vertices. Let us define the value of a subgraph as a maximum of values of its connected components. We will assume that the value of an empty subgraph equals 00.

There is also a value of a kk-coloring, which equals the sum of values of subgraphs of all kk colors. Given a tree, for each kk from 11 to n−1n - 1 calculate the maximal value of a kk-coloring.

你可能听说过赫拉克勒斯的十二项功绩,但你知道第十三项吗?人们普遍认为他花了十二年才完成这十二项壮举,因此平均下来,每项壮举耗时一年。而如今时间流逝得更快了,你只有几分钟(而非数月)来解决本题。你能否成功?

本题中,你将得到一棵含 nn 个带权顶点的树。树是一种具有 n−1n - 1 条边的连通图。

我们定义该树的一个 kk-染色(kk-coloring)为:将 kk 种颜色之一分配给每条边,使得每条边恰好被赋予一种颜色。(注意:你不必使用全部 kk 种颜色。)

颜色为 xx 的子图由原树中所有被染成颜色 xx 的边,以及仅包含至少与一条此类边相邻的顶点所构成。因此,该子图中不存在度为 00 的顶点。

一个连通分量的值定义为该连通分量中所有顶点的权值之和。我们定义一个子图的值为它的所有连通分量的值中的最大值。我们约定:空子图的值为 00。

此外,一个 kk-染色的值定义为所有 kk 种颜色对应的子图的值之和。给定一棵树,请对每个 k=1,2,…,n−1k = 1, 2, \dots, n - 1,计算其 kk-染色所能达到的最大值。

输入格式

In the first line of input, there is a single integer tt (1≤t≤1051 \leq t \leq 10^5) denoting the number of test cases. Then tt test cases follow.

First line of each test case contains a single integer nn (2≤n≤1052 \leq n \leq 10^5). The second line consists of nn integers w1,w2,…,wnw_1, w_2, \dots, w_n (0≤wi≤1090 \leq w_i \leq 10^9), wiw_i equals the weight of ii-th vertex. In each of the following n−1n - 1 lines, there are two integers uu, vv (1≤u,v≤n1 \leq u,v \leq n) describing an edge between vertices uu and vv. It is guaranteed that these edges form a tree.

The sum of nn in all test cases will not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5)。第二行包含 nn 个整数 w1,w2,…,wnw_1, w_2, \dots, w_n(0≤wi≤1090 \leq w_i \leq 10^9),其中 wiw_i 表示第 ii 个顶点的权重。接下来的 n−1n - 1 行中,每行包含两个整数 uu、vv(1≤u,v≤n1 \leq u,v \leq n),表示顶点 uu 与顶点 vv 之间存在一条边。保证这些边构成一棵树。

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

输出格式

For every test case, your program should print one line containing n−1n - 1 integers separated with a single space. The ii-th number in a line should be the maximal value of a ii-coloring of the tree.

对于每个测试用例,你的程序应输出一行,包含 n−1n - 1 个整数,整数之间用单个空格分隔。该行中第 ii 个数应为该树的一个 ii-着色的最大值。

输入输出样例

  • 输入#1

    4
    4
    3 5 4 6
    2 1
    3 1
    4 3
    2
    21 32
    2 1
    6
    20 13 17 13 13 11
    2 1
    3 1
    4 1
    5 1
    6 1
    4
    10 6 6 6
    1 2
    2 3
    4 1

    输出#1

    18 22 25
    53
    87 107 127 147 167
    28 38 44

说明/提示

The optimal kk-colorings from the first test case are the following:

In the 11-coloring all edges are given the same color. The subgraph of color 11 contains all the edges and vertices from the original graph. Hence, its value equals 3+5+4+6=183 + 5 + 4 + 6 = 18.

In an optimal 22-coloring edges (2,1)(2, 1) and (3,1)(3,1) are assigned color 11. Edge (4,3)(4, 3) is of color 22. Hence the subgraph of color 11 consists of a single connected component (vertices 1,2,31, 2, 3) and its value equals 3+5+4=123 + 5 + 4 = 12. The subgraph of color 22 contains two vertices and one edge. Its value equals 4+6=104 + 6 = 10.

In an optimal 33-coloring all edges are assigned distinct colors. Hence subgraphs of each color consist of a single edge. They values are as follows: 3+4=73 + 4 = 7, 4+6=104 + 6 = 10, 3+5=83 + 5 = 8.

第一个测试用例的最优 kk-着色方案如下所示:

在 11-着色中,所有边被赋予同一种颜色。颜色 11 对应的子图包含原图的所有边和顶点,因此其值为 3+5+4+6=183 + 5 + 4 + 6 = 18。

在最优的 22-着色中,边 (2,1)(2, 1) 和 (3,1)(3,1) 被赋予颜色 11,边 (4,3)(4, 3) 被赋予颜色 22。因此,颜色 11 对应的子图由一个连通分量(顶点 1,2,31, 2, 3)构成,其值为 3+5+4=123 + 5 + 4 = 12;颜色 22 对应的子图包含两个顶点和一条边,其值为 4+6=104 + 6 = 10。

在最优的 33-着色中,所有边被赋予互不相同的颜色。因此,每种颜色对应的子图均仅含一条边,其值分别为:3+4=73 + 4 = 7、4+6=104 + 6 = 10、3+5=83 + 5 = 8。

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

首页