hdu 5242 樹鏈剖分找權值最大的前k條鏈,hdu5242

來源:互聯網
上載者:User

hdu 5242 樹鏈剖分找權值最大的前k條鏈,hdu5242

http://acm.hdu.edu.cn/showproblem.php?pid=5242

Problem DescriptionIt 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.
 
InputThe 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).

 
OutputFor 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
/**hdu 5242  樹鏈剖分找權值最大的前k條鏈題目大意:給定一個樹形的遊戲網路,可以從根節點出發k個人,每個人可以沿著一條路徑走下去,不能回頭,出口在各個葉子節點,在路過一個節點時可以          獲得該點的權值,每個點的權值只能被獲得一次,問k個人怎樣走最後可以獲得的權值最多解題思路:首先從反向建立一棵有向樹(從葉子節點到根節點),首先dfs1找出每個節點到根節點的最大權路徑,然後按權值遞減排序,dfs2找每個點到根節點          的最大權路徑,走過的點不能重複走,最後在這些最大權路徑中取前k大即為答案*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int maxn=100005;typedef long long LL;int n,m;LL a[maxn],x[maxn];int head[maxn],ip;bool vis[maxn];void init(){    memset(head,-1,sizeof(head));    ip=0;}struct note{    int v,next;}edge[maxn*2];struct node{    LL sum;    int id;    bool operator <(const node &other)const    {        return sum>other.sum;    }}p[maxn];void addedge(int u,int v){    edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}LL dfs1(int u){    if(vis[u])return p[u].sum;    vis[u]=1;    p[u].sum=a[u];    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        p[u].sum+=dfs1(v);    }    return p[u].sum;}LL dfs2(int u){    if(vis[u])return 0;    vis[u]=1;    LL x=a[u];    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        x+=dfs2(v);    }    return x;}int main(){    int T,tt=0;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)        {            scanf("%I64d",&a[i]);            p[i].id=i;        }        init();        for(int i=1;i<n;i++)        {            int x,y;            scanf("%d%d",&x,&y);            addedge(y,x);        }        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)        {            if(!vis[i])                dfs1(i);        }        sort(p+1,p+n+1);        /*for(int i=1;i<=n;i++)        {            printf("%d:%d\n",p[i].id,p[i].sum);        }*/        memset(x,0,sizeof(x));        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)        {            x[i]=dfs2(p[i].id);        }        sort(x+1,x+n+1);        LL ans=0;        for(int i=n;i>=1&&i>n-m;i--)        {            ans+=x[i];        }        printf("Case #%d: %I64d\n",++tt,ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.