Game(树形dp/找权值最大的前k条链)

 https://cn.vjudge.net/contest/312854#problem/F

It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play kk games simultaneously. 

One day he gets a new gal game named ''XX island''. There are nn scenes in that game, and one scene will be transformed to different scenes by choosing different options while playing the game. All the scenes form a structure like a rooted tree such that the root is exactly the opening scene while leaves are all the ending scenes. Each scene has a value , and we use wiwi as the value of the ii-th scene. Once Katsuragi entering some new scene, he will get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he will get wiwi for only once. 

For his outstanding ability in playing gal games, Katsuragi is able to play the game kk times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for kk times. 

Input

The first line contains an integer TT(T≤20T≤20), denoting the number of test cases. 

For each test case, the first line contains two numbers n,k(1≤k≤n≤100000)n,k(1≤k≤n≤100000), denoting the total number of scenes and the maximum times for Katsuragi to play the game ''XX island''. 

The second line contains nn non-negative numbers, separated by space. The ii-th number denotes the value of the ii-th scene. It is guaranteed that all the values are less than or equal to 231−1231−1. 

In the following n−1n−1 lines, each line contains two integers a,b(1≤a,b≤n)a,b(1≤a,b≤n), implying we can transform from the aa-th scene to the bb-th scene. 

We assume the first scene(i.e., the scene with index one) to be the opening scene(i.e., the root of the tree). 
 

Output

For each test case, output ''Case #t:'' to represent the tt-th case, and then output the maximum total value Katsuragi will get. 

Sample Input

2
5 2
4 3 2 1 1
1 2
1 5
2 3
2 4
5 3
4 3 2 1 1
1 2
1 5
2 3
2 4

Sample Output

Case #1: 10
Case #2: 11

题意:

  1. 给定一个树形的游戏网络,可以从根节点出发k个人,每个人可以沿着一条路径走下去,不能回头,出口在各个叶子节点,在路过一个节点时可以

  2. 获得该点的权值,每个点的权值只能被获得一次,问最后可以获得的权值最大是多少

 

对于一个父节点来说,它肯定要和权值和最大的孩子节点结合,那么其他节点的权值和扔到队列里,最后从大到小排个序,取前k大

树链剖分(轻重链)学习 https://www.acwing.com/blog/content/351/

(秦淮岸大佬的图)

在这个题里其实蓝色的边就是没有,不知道怎么说,自己想想应该能懂 

 

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll val[N];
struct Edge{
	int v,next;
}e[N<<1];
int head[N],tot=0;
void init(int n){
	tot=0;
	for(int i=1;i<=n;i++) head[i]=-1;
}
void add(int u,int v){
	e[tot].v=v;
	e[tot].next=head[u];
	head[u]=tot++;
}
ll dp[N];
ll q[N],tail=0;
void dfs(int u,int fa){
	dp[u]=val[u];
	for(int i=head[u];~i;i=e[i].next){
		int &v=e[i].v;
		if(v==fa) continue;
		dfs(v,u);
		if(dp[u]>dp[v]+val[u]){//那么我们肯定不让u和v在同一条链上更优 
			q[++tail]=dp[v];
		}
		else{  //让u与v在一条链上更优 
			q[++tail]=dp[u]-val[u];
			dp[u]=dp[v]+val[u];
		}
		//这样保证了每个点只有第一次经过的时候才能获得它的权值 
	}
}
bool cmp(ll a,ll b){
	return a>b;
}
int main(){
	ios::sync_with_stdio(false);
    cout.tie(NULL);
    int T;
	scanf("%d",&T);
	int cas=0;
	while(T--){
		int n,k;
		scanf("%d%d",&n,&k);
		init(n);
		tail=0;
		for(int i=1;i<=n;i++) scanf("%lld",&val[i]);
		for(int i=1;i<n;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
		}
		dfs(1,0);
		ll ans=dp[1];
	    sort(q+1,q+1+tail,cmp);
	    for(int i=1;i<k;i++){
	    	ans+=q[i];
		}
		printf("Case #%d: %lld\n",++cas,ans);
	} 
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值