hdu5242 Game 上海邀请赛G题

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.
 

Input
The first line contains an integer   T ( T20 ), denoting the number of test cases.

For each test case, the first line contains two numbers   n,k(1kn100000) , 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   2311 .

In the following   n1  lines, each line contains two integers   a,b(1a,bn) , 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
 

  一棵树N个点,每个点有个权值,玩K次游戏,每次从根走到叶子结点,得到经过节点的权值和,并且吧这些经过节点的权值都变成0,也就是说每个点的权值最多只能得到一次。问K次游戏后得到的最大权值和是多少。

  当时比赛的时候被这题卡了一阵,好在最后做出来了~思路是把从根节点到当前节点的路径的权值和赋给当前节点到叶子节点路径权值和最大的一条, 两次dfs,第一次dfs找到每个节点对应的拥有最大权值和路径的子节点,第二次dfs沿着路径记录权值和sum,如果是对应的最大权值和子节点就把sum传下去,否则传0。把走到叶子节点时把sum存下来,对应一条路径的权值和。最后把这些存下来的路径权值排序,从大到小取前K个。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

typedef long long LL;
const int MAXN=100010;


int T,N,K;
int m[MAXN];
LL a[MAXN];
vector<LL> V,g[MAXN];

LL dfs1(int u,int fa){
    int len=g[u].size();
    if(len<=0) return a[u];
    LL MAX=-1;
    for(int i=0;i<len;i++){
        int v=g[u][i];
        if(v!=fa){
            LL n=dfs1(v,u);
            if(n>MAX){
                MAX=n;
                m[u]=v;
            }
        }
    }
    return MAX+a[u];
}

void dfs2(int u,int fa,LL sum){
    int len=g[u].size();
    if(len<=0) V.push_back(sum+a[u]);
    for(int i=0;i<len;i++){
        int v=g[u][i];
        if(v!=fa){
            if(v==m[u]) dfs2(v,u,sum+a[u]);
            else dfs2(v,u,0);
        }
    }
}

int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    int cas=0;
    while(T--){
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++) scanf("%I64d",&a[i]);
        for(int i=0;i<=N;i++) g[i].clear();
        int u,v;
        for(int i=0;i<N-1;i++){
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
        }
        V.clear();
        dfs1(1,-1);
        dfs2(1,-1,0);
        sort(V.begin(),V.end());
        int len=V.size();
        LL ans=0;
        for(int i=len-1;i>=0&&i>=len-K;i--) ans+=V[i];
        printf("Case #%d: %I64d\n",++cas,ans);
    }
    return 0;
}



你可能感兴趣的:(hdu5242 Game 上海邀请赛G题)