HDU 5242 Game

来源:互联网 发布:成都工作怎么样知乎 编辑:程序博客网 时间:2024/06/06 02:31

题意:一棵树有n个结点,n-1条边,每个结点有个权值。每次可以获得从根节点走到叶子结点所有结点的权值和,但是每个结点的权值只能使用一次。求走k次所能获得的最大权值和

思路:首先dfs一次求出所有叶子结点到根结点的权值和,然后从大到小排序,然后根据这个顺序再一次dfs求出每个结点到根节点的权值和,然后再次排序选前k个,证明:每次选择一个叶子结点走到根节点,相当于每次取一条单链,对于有交叉的两条链,先选权值大的肯定是最优的,因为对于某条跟它们没有交叉的链来说,这样子的操作并不会影响到它


#include<bits/stdc++.h>using namespace std;const int maxn = 100010;#define LL long longint n,k;LL a[maxn],ans[maxn];int vis[maxn];vector<int>e[maxn];struct Node{LL sum;int idx;bool operator<(const Node&a)const{return sum>a.sum;}}nodes[maxn];bool cmp(LL a,LL b){return a>b;}LL dfs1(int u){if (vis[u])return nodes[u].sum;nodes[u].sum = a[u];vis[u]=1;for (int i = 0;i<e[u].size();i++){int v = e[u][i];nodes[u].sum+=dfs1(v);}return nodes[u].sum;}LL dfs2(int u){if (vis[u])return 0;LL temp = a[u];vis[u]=1;for (int i = 0;i<e[u].size();i++){int v = e[u][i];temp+=dfs2(v);}return temp;}int main(){    int T,cas=1;scanf("%d",&T);while(T--){scanf("%d%d",&n,&k);for (int i = 1;i<=n;i++){scanf("%lld",&a[i]);nodes[i].idx = i;}for (int i = 0;i<=n;i++)e[i].clear();memset(vis,0,sizeof(vis));for (int i = 1;i<n;i++){int u,v;scanf("%d%d",&u,&v);//e[u].push_back(v);e[v].push_back(u);}for (int i = 1;i<=n;i++)if (!vis[i])dfs1(i);sort(nodes+1,nodes+1+n);memset(vis,0,sizeof(vis));for (int i = 1;i<=n;i++)            ans[i] = dfs2(nodes[i].idx);sort(ans+1,ans+1+n,cmp);int cnt = 0;LL anss = 0;for (int i = 1;i<=n;i++){             anss+=ans[i]; cnt++; if (cnt==k) break;}printf("Case #%d: %lld\n",cas++,anss);}}


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 $w_i$ 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 $w_i$ 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 \le 20$), denoting the number of test cases. 

For each test case, the first line contains two numbers $n, k(1 \le k \le n \le 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 $2^{31} - 1$. 

In the following $n - 1$ lines, each line contains two integers $a, b(1 \le a, b \le 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

25 24 3 2 1 11 21 52 32 45 34 3 2 1 11 21 52 32 4
 

Sample Output

Case #1: 10Case #2: 11
 


0 0
原创粉丝点击