Game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 574 Accepted Submission(s): 163
Problem Description
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
k
games simultaneously.
One day he gets a new gal game named ''XX island''. There are n 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 wi as the value of the i -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 wi for only once.
For his outstanding ability in playing gal games, Katsuragi is able to play the game k times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for k times.
One day he gets a new gal game named ''XX island''. There are n 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 wi as the value of the i -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 wi for only once.
For his outstanding ability in playing gal games, Katsuragi is able to play the game k times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for k times.
Input
The first line contains an integer
T
(
T≤20
), denoting the number of test cases.
For each test case, the first line contains two numbers 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 n non-negative numbers, separated by space. The i -th number denotes the value of the i -th scene. It is guaranteed that all the values are less than or equal to 231−1 .
In the following n−1 lines, each line contains two integers a,b(1≤a,b≤n) , implying we can transform from the a -th scene to the b -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).
For each test case, the first line contains two numbers 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 n non-negative numbers, separated by space. The i -th number denotes the value of the i -th scene. It is guaranteed that all the values are less than or equal to 231−1 .
In the following n−1 lines, each line contains two integers a,b(1≤a,b≤n) , implying we can transform from the a -th scene to the b -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
t
-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次,每次都到根节点,走过的不能再加,问最大值
思路:类似于链剖分,祖先节点的权值只加在重链上
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
int N,K;
int a[maxn];
vector<int> g[maxn];
vector<LL> ans;
int son[maxn];
LL dfs1(int u,int fa)
{
int len=g[u].size();
if(len==0)return a[u];
LL maxv=0;
for(int i=0;i<len;i++)
{
int v=g[u][i];
if(v==fa)continue;
LL tmp=dfs1(v,u);
if(tmp>maxv)
{
maxv=tmp;
son[u]=v;
}
}
return maxv+a[u];
}
void dfs2(int u,int fa,LL sum)
{
int len=g[u].size();
if(len==0)
{
ans.push_back(sum+a[u]);
return ;
}
for(int i=0;i<len;i++)
{
int v=g[u][i];
if(v==fa)continue;
if(v==son[u])dfs2(v,u,sum+a[u]);
else dfs2(v,u,0);
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
for(int i=1;i<=N;i++)scanf("%d",&a[i]);
for(int i=0;i<=N;i++)g[i].clear();
ans.clear();
for(int i=1;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
}
dfs1(1,0);
dfs2(1,0,0);
sort(ans.begin(),ans.end());
LL sum=0;
int len=ans.size();
for(int i=len-1;i>=max(len-K,0);i--)sum+=ans[i];
printf("Case #%d: %I64d\n",cas++,sum);
}
return 0;
}