hdu 5242——Game——————【树链剖分思想】
Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
System Crawler (2015-05-26)
Description
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
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
Sample Input
Sample Output
Case #2: 11
#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——————【树链剖分思想】的更多相关文章
- HDU 5242 利用树链剖分思想进行贪心
题目大意: 在给定带权值节点的树上从1开始不回头走到某个底端点后得到所有经过的点的权值后,这些点权值修改为0,到达底部后重新回到1,继续走,问走k次,最多能得到多少权值之和 这其实就是相当于每一次都走 ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- HDU 5242 树链剖分思想的贪心
题意及博客 树链剖分分为2步,第一次求出深度,重儿子,第二次求出重链,用到了启发式的思想,即对于比较重的儿子,尽量去完整的维护它.类似于我们去合并两个堆,明显把小的堆逐个插入大的堆中会比大的往小的插更 ...
- HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...
- HDU 3966(树链剖分+点修改+点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题目大意:营地的分布成树型.每个营地都有一些人,每次修改修改一条链上的所有营地的人数,每次查询单 ...
- HDU 5458 Stability (树链剖分+并查集+set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...
- HDU 5044 Tree --树链剖分
题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...
- HDU - 3966-Aragorn' Story(树链剖分+线段树)
链接:https://vjudge.net/problem/HDU-3966 题意: Our protagonist is the handsome human prince Aragorn come ...
- HDU 3966 RE 树链剖分 线段树 Aragorn's Story
题意: 给出一棵树,每个顶点上有一个权值. 操作:选择一条路径,并将路径上所有的点的权值同时加或减某个数. 查询:某个点的当前权值 分析: 树链剖分完毕后,就是简单的线段树区间更新. 提交的时候注意要 ...
- Tree HDU - 6547 (树链剖分,线段树)
wls 有三棵树,树上每个节点都有一个值 ai,现在有 2 种操作: 将一条链上的所有节点的值开根号向下取整: 求一条链上值的和: 链的定义是两点之间的最短路. Input 第一行两个数 n, q 分 ...
随机推荐
- 《C#多线程编程实战》2.4 SemaphoreSlim
这个简单多了. 理解也是很好理解. 比上一个mutex好理解多了. 这个SemaphoreSlim是干什么呢? 就是限制线程的来访问. 好比说一次只有两个,一次只有三个 这样的线程来访问资源. 有点 ...
- [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛A
[SinGuLaRiTy-1036] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 小C的倍数问题 Time Limit: 2000/100 ...
- 题解 CF500D 【New Year Santa Network】
题目链接 这道题首先是要看看该如何化简,先把三元组化成二元组. 之后统计经过某条边的 次数$*$权值 的和. 最后除以总基数 $tot$ 其中,每条边被计算的次数为 子树的点数$*$非子树的点数 ( ...
- HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组
本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y) 实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大, ...
- TX2 Clone
由于给TX2配置了很多的开发环境,也修改了一些驱动,想将这些环境能够完整的迁移到一块bare TX2,于是尝试了clone的方法. 这种方法的优点是: 确保了移植的TX2 与已经配置好的环境是一致的: ...
- vector<vector<int>> 使用简单示例
#include <iostream> #include <vector> using namespace std; int main() { vector<vector ...
- undefined reference to symbol ‘_ZN2cv6String10deallocateEv
使用qt编译Caffe时出现如下错误: undefined reference to symbol '_ZN2cv6String10deallocateEv' error adding symbols ...
- Mysql Update更新错误 Error Code:1175
Mysql 5.7,默认执行 update 语句时遇到错误提示: Error Code: 1175. You are using safe update mode and you tried to u ...
- ASP.NET中MD5的加密方式很简单
在ASP.NET中MD5的加密方式很简单,代码如下: FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5&quo ...
- Codeforces - 185A 简单矩阵快速幂
题意:求第n个三角形内部的上三角形个数 对每个三角形分别维护上下三角形个数,记为\(dp[1][i],dp[2][i]\) 规律很明显是 \(dp[1][i+1]=3*dp[1][i]+dp[2][i ...