Game

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

Appoint description: 
System Crawler  (2015-05-26)

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\n",++cnt,ans);
while(!Q.empty())
Q.pop();
for(i=0;i<=n;i++){
V[i].clear();
}
}
return 0;
}

  

hdu 5242——Game——————【树链剖分思想】的更多相关文章

  1. HDU 5242 利用树链剖分思想进行贪心

    题目大意: 在给定带权值节点的树上从1开始不回头走到某个底端点后得到所有经过的点的权值后,这些点权值修改为0,到达底部后重新回到1,继续走,问走k次,最多能得到多少权值之和 这其实就是相当于每一次都走 ...

  2. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  3. HDU 5242 树链剖分思想的贪心

    题意及博客 树链剖分分为2步,第一次求出深度,重儿子,第二次求出重链,用到了启发式的思想,即对于比较重的儿子,尽量去完整的维护它.类似于我们去合并两个堆,明显把小的堆逐个插入大的堆中会比大的往小的插更 ...

  4. HDU 5044 (树链剖分+树状数组+点/边改查)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...

  5. HDU 3966(树链剖分+点修改+点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题目大意:营地的分布成树型.每个营地都有一些人,每次修改修改一条链上的所有营地的人数,每次查询单 ...

  6. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  7. HDU 5044 Tree --树链剖分

    题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...

  8. HDU - 3966-Aragorn' Story(树链剖分+线段树)

    链接:https://vjudge.net/problem/HDU-3966 题意: Our protagonist is the handsome human prince Aragorn come ...

  9. HDU 3966 RE 树链剖分 线段树 Aragorn's Story

    题意: 给出一棵树,每个顶点上有一个权值. 操作:选择一条路径,并将路径上所有的点的权值同时加或减某个数. 查询:某个点的当前权值 分析: 树链剖分完毕后,就是简单的线段树区间更新. 提交的时候注意要 ...

  10. Tree HDU - 6547 (树链剖分,线段树)

    wls 有三棵树,树上每个节点都有一个值 ai,现在有 2 种操作: 将一条链上的所有节点的值开根号向下取整: 求一条链上值的和: 链的定义是两点之间的最短路. Input 第一行两个数 n, q 分 ...

随机推荐

  1. 在构造函数和析构函数中调用虚函数------新标准c++程序设计

    在构造函数和析构函数中调用虚函数不是多态,因为编译时即可确定调用的是哪个函数.如果本类有该函数,调用的就是本类的函数:如果本类没有,调用的就是直接基类的函数:如果基类没有,调用的就是间接基类的函数,以 ...

  2. 关于命名空间 namespace的总结

    namespace 有作用的类型  类.函数.常量关键字namespace必须在所有代码之前 除用于编码的declare语句 namespace Myproject; const A = 1; cla ...

  3. Kubernetes 集群部署(4) -- Node 部署

    以下无特殊说明,都是在 Node 节点运行 1. 创建文件 vim /opt/k8s/cfg/kubelet.conf,内容如下: KUBELET_OPTS="--logtostderr=t ...

  4. 「BZOJ1433」[ZJOI2009] 假期的宿舍(二分图,网络流)

    题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 C 来看B,C 与 A 不认识. ...

  5. Java面向对象之关键字this 入门实例

    一.基础概念 1.关键字this是指:哪个对象调用this所在的函数.this就指向当前这个对象. 2.用法: (1).this关键字可以解决:构造函数私有化问题. 注意:构造函数只能被构造函数调用, ...

  6. Spark Programming Guide《翻译》

    转载必须注明出处:梁杰帆 在这里要先感谢原作者们!如果各位在这里发现了错误之处,请大家提出 1.Initializing Spark     Spark程序必须做的第一件事就是创建一个SparkCon ...

  7. 搭建git服务器(临时服务器,命令行形式,针对2到5人左右,轻量)

    服务端配置 ############################################################################################## ...

  8. 为什么我引用bootstrap的font-awesome的图标不出来,就单单是一个HTML里面引用的?

    原文地址:https://www.zhihu.com/question/37015526?sort=created 24 个回答 知乎用户     我来猜猜: 1.检查字体路径是否加载对了2.font ...

  9. [转] Nook Glowlight Plus入门指南(Root及相关软件设置)

    [From] http://www.dakang.info/nook-glowlight-plus-root/ 本文仅仅是对大神 xukong及众多热心Hper帖子的一个汇总,稍作个人补充,方便查询, ...

  10. Oracle KEEP的用法

    [摘录自] http://blog.itpub.net/12932950/viewspace-687036/ http://flyfx.iteye.com/blog/1994993 聚合函数MIN, ...