G - Game HDU - 5242 (dfs)

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(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).

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
题意:一棵带权值树,你可以多次从根节点走到叶子节点,走过的地方就变为0,问走多次后,可以获得最大权值是多少

思路:题目说了只有n-1条边,所以可以推得,到每个叶子节点有且只有一条边,所以可以先一个dfs
把每条根节点到叶子节点的路的权值和算出来,dfs只要dfs1次,所以很快,不会超时

void dfs(int x)
{
    for(int i=0;i<v[x].size();i++){
        a[v[x][i]].val+=a[x].val;
        dfs(v[x][i]);
    }
}

第二步,将节点按权值排序,优先遍历权值大的点(一种贪心的思路),然后从权值和大的节点遍历到根节点,将访问的点标记,下次就不能走了

LL dfs1(int x,LL val)
{
    if(x==1 && !vis[1]){
        vis[1]=1;
        return Val[1];
    }
    if(x==1 || vis[x]==1)
        return 0;
    vis[x]=1;
    for(int i=0;i<v1[x].size();i++){
        if(vis[v1[x][i]]==1)
            return val;
        else{
            return val+dfs1(v1[x][i],Val[v1[x][i]]);
        }
    }
}

最后排序,加上前k个值就是答案
完整代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
int t;
int n,k;
struct node
{
    int ind;
    LL val;
}a[Max];
vector<int>v[Max],v1[Max];
LL Val[Max];
int vis[Max];
void dfs(int x)
{
    for(int i=0;i<v[x].size();i++){
        a[v[x][i]].val+=a[x].val;
        dfs(v[x][i]);
    }
}
LL dfs1(int x,LL val)
{
    if(x==1 && !vis[1]){
        vis[1]=1;
        return Val[1];
    }
    if(x==1 || vis[x]==1)
        return 0;
    vis[x]=1;
    for(int i=0;i<v1[x].size();i++){
        if(vis[v1[x][i]]==1)
            return val;
        else{
            return val+dfs1(v1[x][i],Val[v1[x][i]]);
        }
    }
}
int cmp(node a,node b)
{
    return a.val>b.val;
}
int main()
{
    scanf("%d",&t);
    for(int T=1;T<=t;T++)
    {
        scanf("%d%d",&n,&k);
        int x,y;
        for(int i=1; i<=n; i++){
            scanf("%lld",&Val[i]);
            a[i].ind=i;
            a[i].val=Val[i];
            v[i].clear();
            v1[i].clear();
        }
        for(int i=0; i<n-1; i++)
        {
            scanf("%d%d",&x,&y);
            v1[y].push_back(x);
            v[x].push_back(y);
        }
        dfs(1);
        memset(vis,0,sizeof(vis));
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n && a[i].ind!=0;i++)
        {
            a[i].val=dfs1(a[i].ind,Val[a[i].ind]);
        }
        sort(a+1,a+n+1,cmp);
        LL Ans=0;
        for(int i=1;i<=k;i++)
            Ans+=a[i].val;
        printf("Case #%d: %lld\n",T,Ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Gee_Zer/article/details/89307673