zoukankan      html  css  js  c++  java
  • hdu 5242——Game——————【树链剖分思想】

    Game
    Time Limit:1500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Appoint description: 

    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 $w_i$ 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 $w_i$ 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 le 20$), denoting the number of test cases. 

    For each test case, the first line contains two numbers $n, k(1 le k le n le 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 $2^{31} - 1$. 

    In the following $n - 1$ lines, each line contains two integers $a, b(1 le a, b le 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
     
     
    题目大意:给你一棵树,有n个结点,有n-1条边,每个结点都有一个价值可以从跟到叶子k次,但是只能拿一次价值,问你从根1到达叶子的最大价值。
     
    解题思路:用深搜划分出与根临接的顶点个数数量的子树,回溯的时候从这些子树中可以找出重链,如果在中间有不是重链上的链,则把该链放入优先队列中,否则维护出一条重链。dp[i]表示从叶子到i结点的最大价值和。
     
     
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    #define LL __int64
    const int maxn=1e5+100;
    vector<LL>V[maxn];
    priority_queue<LL>Q;
    LL a[maxn];
    LL dp[maxn];
    LL dfs(int cur){
        int i,v;
        LL tmp;
        dp[cur]=a[cur];
        for(i=0;i<V[cur].size();i++){
            v=V[cur][i];
            tmp=dfs(v);
            if(dp[cur]>tmp+a[cur]){ //如果有其他轻链,把轻链放入队列
                Q.push(tmp);
            }else{      //如果当前这个链不是最重的链,把从叶子到该结点的这段链放入队列
                Q.push(dp[cur]-a[cur]);
                dp[cur]=tmp+a[cur];
            }
        }
        return dp[cur];
    }
    int main(){
    	int t ,n,m,i,j,k,u,v,cnt=0;
    	LL ans;
    	scanf("%d",&t);
    	while(t--){
    		scanf("%d%d",&n,&k);
    		for(i=1;i<=n;i++)
    			scanf("%lld",&a[i]);
    		for(i=1;i<n;i++){
    			scanf("%d%d",&u,&v);
    			V[u].push_back(v);
    		}
    		dfs(1);
    		ans=dp[1];
    		for(i=1;i<k;i++){
    			ans+=Q.top();
    			Q.pop();
    		}
    		printf("Case #%d: %lld
    ",++cnt,ans);
    		while(!Q.empty())
    			Q.pop();
    		for(i=0;i<=n;i++){
    			V[i].clear();
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    spark 源码分析之五--Spark RPC剖析之创建NettyRpcEnv
    spark 源码分析之四 -- TaskScheduler的创建和启动过程
    spark 源码分析之三 -- LiveListenerBus介绍
    spark 源码分析之二 -- SparkContext 的初始化过程
    scala class中孤立代码块揭秘
    spark 源码分析之一 -- RDD的四种依赖关系
    spark streaming 接收kafka消息之五 -- spark streaming 和 kafka 的对接总结
    spark streaming 接收kafka消息之四 -- 运行在 worker 上的 receiver
    spark streaming 接收kafka消息之三 -- kafka broker 如何处理 fetch 请求
    spark streaming 接收kafka消息之二 -- 运行在driver端的receiver
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4546933.html
Copyright © 2011-2022 走看看