HDU - 5242 Game (树链剖分 OR dfs+贪心 待整理)

Game

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


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
 

Source
 

Recommend

借鉴:here

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

dfs1求出所有结点到根节点的权值和,然后按从大到小排序,根据这个顺序,dfs2求出每个结点到根节点的权值和,遍历过的结点的权值不能用。 
然后再从大到小排个序,取前面的k个

每次选择一个叶子结点走到根节点,相当于每次取一条单链,对于有交叉的两条链,先选权值大的肯定是最优的,因为对于某条跟它们没有交叉的链来说,这样子的操作并不会影响到它

#include <bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
const int maxn = 100010;
using namespace std;
typedef long long LL;
int n,m;
LL a[maxn],x[maxn];
bool vis[maxn];
struct Edge{
    int to,next;
}edge[maxn<<1];int head[maxn],tot;
void init()
{
    tot=0;
    clr(head,0xff);
}
void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
struct Node{
    LL sum;
    int id;
    bool operator<(const Node&a)const{
        return sum>a.sum;
    }
}node[maxn];
LL dfs1(int u)
{
    if(vis[u]) return node[u].sum;
    node[u].sum=a[u];
    vis[u]=true;
    for(int i=head[u];~i;i=edge[i].next){
        int v=edge[i].to;
        node[u].sum+=dfs1(v);
    }
    return node[u].sum;
}
LL dfs2(int u)
{
    if(vis[u]) return 0;
    LL w=a[u];
    vis[u]=true;
    for(int i=head[u];~i;i=edge[i].next){
        int v=edge[i].to;
        w+=dfs2(v);
    }
    return w;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        clr(node,0);
        for(int i=1;i<=n;++i){
            scanf("%I64d",&a[i]);
            node[i].id=i;
        }
        clr(vis,false);
        init();
        int u,v;
        for(int i=1;i<n;++i){
            scanf("%d%d",&u,&v);
            addedge(v,u);
        }
        for(int i=1;i<=n;++i){
            if(!vis[i]){
                dfs1(i);
            }
        }
//        cout<<"-----"<<endl;
        sort(node+1,node+n+1);
        clr(vis,false);
        for(int i=1;i<=n;++i){
            x[i]=dfs2(node[i].id);
        }

        LL ans=0;
        sort(x+1,x+n+1,greater<LL>());
//        for(int i=1;i<=n;++i) cout<<x[i]<<" ";cout<<endl;
        for(int i=1,cnt=0;i<=n,cnt<m;++i)
            ans+=x[i],cnt++;
        printf("Case #%d: %I64d\n",cas++,ans);
    }
    return 0;
}


借鉴here

题意:

给定一颗以1号节点为根节点的有向树,每个节点有一个权值,问从1号节点出发k次,能到达的所有节点的和的最大值。

思路:

贪心的把树的链按照权值和从大到小剖分成若干条链(过程可以根据上交书上的熟练剖分模版做细小改动),然后根据每条链的权值排序取最大的k个就是答案。

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn =100000+5;
const int maxm=maxn+maxn;
int v[maxm],info[maxn],Prev[maxm],Q[maxn],dep[maxn];
int belong[maxn],head[maxn];
long long size[maxn];
bool vis[maxn];
int cnt=0,N,nedge=0;
int weight[maxn];
inline void insert(int x,int y){
    ++nedge;
    v[nedge]=y;Prev[nedge]=info[x];info[x]=nedge;
}
void split(){
    int l,r;
    memset(dep,-1,sizeof(dep));
    l=0;
    dep[Q[r=1]=1]=0;
    while(l<r){
        int x=Q[++l];
        vis[x]=false;
        for(int y=info[x];y!=-1;y=Prev[y]){
            if(dep[v[y]]==-1){
                dep[Q[++r]=v[y]]=dep[x]+1;
            }
        }
    }
    for(int i=N;i;i--){
        int x=Q[i],p=-1;
        size[x]=weight[x];
        for(int y=info[x];y!=-1;y=Prev[y]){
            if(vis[v[y]]){
                if(p==-1||size[v[y]]>size[p])
                    p=v[y];
            }
        }
        if(p==-1){
            ++cnt;
            belong[head[cnt]=x]=cnt;
        }
        else {
            size[x]+=size[p];
            belong[x]=belong[p];
            head[belong[x]]=x;
        }
        vis[x]=true;
    }

}
bool cmp(int a,int b){
    return size[head[a]]>size[head[b]];
}
int ans[maxn];
long long get_ans(int k){
    long long ret=0;
    for(int i=1;i<=cnt;i++) ans[i]=i;
    sort(ans+1,ans+cnt+1,cmp);
    for(int i=1;i<=min(cnt,k);i++){
        ret+=size[head[ans[i]]];
    }
    return ret;
}
int main(){
    int k,a,b,T,cas=0;
    //freopen("data.in","r",stdin);
    scanf("%d",&T);
    while(T--){
        printf("Case #%d: ",++cas);
        scanf("%d%d",&N,&k);
        memset(info,-1,sizeof(info));
        cnt=0;nedge=0;
        for(int i=1;i<=N;i++){
            scanf("%d",weight+i);
        }
        for(int i=1;i<N;i++){
            scanf("%d%d",&a,&b);
            insert(a,b);
        }
        split();
        cout<<get_ans(k)<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值