HDU5242:Game(树上贪心)

来源:互联网 发布:见过最漂亮的女生知乎 编辑:程序博客网 时间:2024/05/19 07:07

Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1465    Accepted Submission(s): 477


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
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
 

Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest
题意:给一颗N个节点的树,有N-1条有向边,每个点有一个权值,从1号点放k个人走到叶子结点,求覆盖路径的最大权值和。

思路:先反向建树,求出每个点到根节点的权值和,可以保证这是一条无分岔的路。然后从大到小排序,从权值和大的点开始覆盖道路,最后再排序取前k大的路即可。

# include <iostream># include <cstdio># include <cstring># include <algorithm># define ll long longusing namespace std;const int maxn = 1e5;int cnt, cas=1;ll val[maxn+3], ne[maxn+3], vis[maxn+3], road[maxn+3], id[maxn+3];struct node{    int to, next;}a[maxn+3];void add_edge(int u, int v){    a[cnt].to = v;    a[cnt].next = ne[u];    ne[u] = cnt++;}ll dfs(int u)//所有点到根节点的权值和。{    if(vis[u]) return road[u];    vis[u] = 1;    road[u] = val[u];    for(int i=ne[u]; i!=-1; i=a[i].next)    {        int v = a[i].to;        road[u] += dfs(v);    }    return road[u];}ll dfs2(int u){    if(vis[u]) return 0;    vis[u] = 1;    ll tmp = val[u];    for(int i=ne[u]; i!=-1; i=a[i].next)    {        int v = a[i].to;        tmp += dfs2(v);    }    return tmp;}bool cmp(int x, int y){    return road[x] > road[y];}int main(){    int t, n, k, a, b;    scanf("%d",&t);    while(t--)    {        cnt = 0;        memset(ne, -1, sizeof(ne));        scanf("%d%d",&n,&k);        for(int i=1; i<=n; ++i)            scanf("%lld",&val[i]);        for(int i=1; i<n; ++i)        {            scanf("%d%d",&a,&b);            add_edge(b, a);        }        memset(vis, 0, sizeof(vis));        memset(road, 0, sizeof(road));        for(int i=1; i<=n; ++i)        {            id[i] = i;            road[i] = dfs(i);        }        sort(id+1, id+1+n, cmp);        memset(vis, 0, sizeof(vis));        memset(road, 0, sizeof(road));        for(int i=1; i<=n; ++i)        {            int now = id[i];            road[i] = dfs2(now);        }        sort(road+1, road+1+n, greater<ll>());        ll ans = 0;        for(int i=1; i<=k; ++i)            ans += road[i];        printf("Case #%d: %lld\n",cas++, ans);    }    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 台达plc禁止上传怎么办? 潜水泵电机启动绕组断线了怎么办? 永磁电机磁没了怎么办 热水器热水管坏了怎么办 松下伺服驱动器坏了怎么办 步进电机功率小了怎么办 电三轮电机坏了怎么办 电动车钢圈撞变形了怎么办 电动车后轮钢圈变形了怎么办 软油管接头渗油怎么办 一只单眼皮一只双眼皮怎么办 儿童轻轻泥干了怎么办 5d轻轻泥干了怎么办 手机炉石一直闪退怎么办 鸟之羽任务失败怎么办 巫师3没血没药怎么办 荣威rx5灯光不亮怎么办 点滴打没了回血怎么办 加了低标号的油怎么办 别克车钥匙丢了怎么办 霜子哀伤断了怎么办 侧车窗外有雨水怎么办 昂科威15t变速箱异响怎么办 别克昂科拉一公里9毛怎么办? 雷诺科雷傲车钥匙丢了怎么办 奥迪a6l烧机油了怎么办 卡地亚手镯刮花怎么办 卡地亚戒指花了怎么办 手表摔了不走了怎么办 ck手表表扣很难打开怎么办 小天才泡了水怎么办 小天才手表掉水里了怎么办 小天才电话手表进水了怎么办 小天才手表进水了怎么办 小天才电话手表丢了怎么办 小天才电话手表黑屏怎么办 安全守护注册码忘记了怎么办 儿童安全锁的门打不开怎么办 守护宝老年机打不开了怎么办 小米电话手表坏了怎么办 小米手表带坏了怎么办