hdu 5242 树链剖分找权值最大的前k条链
来源:互联网 发布:mt4顾比均线指标源码 编辑:程序博客网 时间:2024/06/06 09:03
http://acm.hdu.edu.cn/showproblem.php?pid=5242
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 aren 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 gamek times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for k times.
One day he gets a new gal game named ''XX island''. There are
For his outstanding ability in playing gal games, Katsuragi is able to play the game
Input
The first line contains an integer T (T≤20 ), denoting the number of test cases.
For each test case, the first line contains two numbersn,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 containsn 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 followingn−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).
For each test case, the first line contains two numbers
The second line contains
In the following
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
/**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;}
0 0
- hdu 5242 树链剖分找权值最大的前k条链
- HDU 5242Game 树上的贪心 树形dp 求出使K条链的权值总和最大
- 01背包(求前一个的最大价值-->求前K个的最大价值) 之 hdu 2639
- 找出最大的前K个数
- 两个数组的元素之和最大的前k项
- 取前k个出现频率最大的单词
- 100w个数中找出最大的前k个数
- 100w个数中找出最大的前K个数
- 利用堆求最大或最小的前k个数
- Top K问题(求前k个最大的数)
- 典型的Top K算法_找出一个数组里面前K个最大数
- HDU 5102 The K-th Distance 队列(树的前k路径)
- 编程之美上的寻找最大的前k个数的算法实现
- hud4807 Lunch Time 【费用流 求前k条不相交的最短路】
- 堆的应用——在N个数中找到最大的前K个数
- 【数据结构】找出N个数据中最大的前k个数据(利用堆排序)
- 【海量数据处理】N个数中找出最大的前K个数
- 【数据结构】找出N个数据中最大的前k个数据(利用堆排序)
- Oracle未启用Partitioning功能解决
- 使用Delphi实现JNI - 实例
- 如何查看python selenium的api
- Sicily 1920 Divide The Stones
- 抽象类与接口的区别
- hdu 5242 树链剖分找权值最大的前k条链
- __stdcall,__cdecl,_cdecl,_stdcall,。__fastcall,_fastcall 区别简介
- 机器学习实战
- 人脸表情识别文献阅读
- poj 3276
- 轮询算法设计及其代码框架
- struts2标签中<s:textfield>中label不显示
- db2常用命令大全
- xbmc 从android_main启动时的部分步骤记录