codeforces Good Bye 2020 D. 13th Labour of Heracles

该博客讨论了一个关于树的着色问题,其中每个边可以被染色,并且每种颜色的子图的值是其包含顶点权重之和的最大值。博主提出了一种使用优先队列的方法来计算每种颜色的最大子图价值,从而解决这个问题。文章通过一个实例展示了如何计算不同颜色的最优着色方案,并给出了相应的输出示例。
摘要由CSDN通过智能技术生成

题目链接:https://codeforc.es/contest/1466/problem/D

outputstandard output
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 n weighted vertices. A tree is a connected graph with n−1 edges.

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

A subgraph of color x consists of these edges from the original tree, which are assigned color x, and only those vertices that are adjacent to at least one such edge. So there are no vertices of degree 0 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 0.

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

Input
In the first line of input, there is a single integer t (1≤t≤105) denoting the number of test cases. Then t test cases follow.

First line of each test case contains a single integer n (2≤n≤105). The second line consists of n integers w1,w2,…,wn (0≤wi≤109), wi equals the weight of i-th vertex. In each of the following n−1 lines, there are two integers u, v (1≤u,v≤n) describing an edge between vertices u and v. It is guaranteed that these edges form a tree.

The sum of n in all test cases will not exceed 2⋅105.

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

Example

input

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

output

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

分析

每多一种颜色,就会有一个点的值会多加一次,设一个点的度为 x ,那么他的 w 最多会被加 x - 1 次,我们只要用优先队列维护这些值,从大到小加这些数,就能得到答案。

Happy new year !

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 200007;
int du[N];
int w[N];
priority_queue<int, vector<int>, less<int> > q;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll sum = 0;
		while(!q.empty()) q.pop();
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&w[i]), du[i] = 0, sum += w[i];
		for(int i=1;i<n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			du[u]++;
			du[v]++;
			if(du[u] > 1) q.push(w[u]);
			if(du[v] > 1) q.push(w[v]);
		}
		printf("%lld ",sum);
		while(!q.empty())
		{
			sum += q.top();
			q.pop();
			printf("%lld ",sum);
		}
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值