HDU 5242 Game(三个贪心)
Game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1385 Accepted Submission(s): 452
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.
For each test case, the first line contains two numbers n,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 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 231−1.
In the following n−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).
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
Case #2: 11
/*
* @Author: lyuc
* @Date: 2017-04-29 19:33:34
* @Last Modified by: lyuc
* @Last Modified time: 2017-04-29 21:16:49
*/ /* 题意:一棵树有n个节点,每个节点有一个价值。一个人从根节点走到某叶子节点算一次游戏,可以
* 获得经过节点的所有价值。但每个节点的价值只能被获得一次。问在同一棵树上进行K次游戏
* ,最多能获得多少价值。
*
* 思路:首先从叶节点开始记录每个叶子节点到达根节点的权值和,按照权值排序,然后按照这个顺序
* 再按照权值排序,然后在统计叶子节点到达根节点的权值和,不过这次每个节点的值只能取一
* 次,然后再按照权值和排序,在取前k个叶子节点的值
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#define LL long long
#define MAXN 100005
using namespace std;
struct Node{
int id;
LL val;
bool operator <(const Node& other) const{
return val>other.val;
}
}node[MAXN];
int t;
int n,k;
int u,v;
LL res;
LL val[MAXN];
int pos[MAXN];//记录那个定点是哪个
vector<int>edge[MAXN];//从根节点开始建树
LL dfs1(int u){//第一遍dfs,统计每个节点到根节点的值
if(node[u].val)
return node[u].val;
LL res=val[u];
for(int i=;i<edge[u].size();i++){
int v=edge[u][i];
res+=dfs1(v);
}
node[u].val+=res;
}
LL dfs2(int u){//第二遍dfs,统计每个节点到根节点的值,每个节点的值只取一次
if(node[pos[u]].val)
return ;
node[pos[u]].val=val[u];
for(int i=;i<edge[u].size();i++){
int v=edge[u][i];
node[pos[u]].val+=dfs2(v);
}
return node[pos[u]].val;
}
void init(){
for(int i=;i<MAXN;i++){
edge[i].clear();
node[i].val=;
}
res=;
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
for(int ca=;ca<=t;ca++){
init();
printf("Case #%d: ",ca);
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)//节点的值
scanf("%lld",&val[i]);
for(int i=;i<n;i++){//建边
scanf("%d%d",&u,&v);
edge[v].push_back(u);
}
for(int i=;i<=n;i++){
node[i].id=i;//节点初始化
pos[i]=i;
if(node[i].val==)
dfs1(node[i].id);
}
sort(node+,node+n+);//给节点排序用于贪心
for(int i=;i<=n;i++){//将节点的值清零
node[i].val=;
pos[node[i].id]=i;
}
for(int i=;i<=n;i++){
if(node[i].val==){
dfs2(node[i].id);
}
}
sort(node+,node+n+);
for(int i=;i<=n;i++){
node[i].val=;
pos[node[i].id]=i;
}
for(int i=;i<=n&&k;i++){
if(node[i].val==){
dfs2(node[i].id);
k--;
res+=node[i].val;
}
}
printf("%lld\n",res);
}
return ;
}
HDU 5242 Game(三个贪心)的更多相关文章
- G - Game HDU - 5242 (数链剖分)
题目链接: G - Game HDU - 5242 题目大意:首先是T组测试样例,给出一颗以1节点为根的树,每个节点有各自的价值,有m次从根节点出发向下走到叶子节点的机会,每次会得到所有经过节点的权值 ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- hdu 5242——Game——————【树链剖分思想】
Game Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 5242 Game(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=5242 题意: 给出一棵树,每个节点都有一个权值,每次可以获得从根结点(1)到叶子节点上的所有权值和,每个节点只能 ...
- HDU 5242 Game(树上贪心)
题目链接 Game 题目的意思很简单, 就是要找一棵树权值最大等等前K条链. 在本题中,走的次数等于min(叶子结点个数,k) tree[i].sum意为从i号结点出发走到某个叶子结点能得到的最大总价 ...
- HDU 5242 利用树链剖分思想进行贪心
题目大意: 在给定带权值节点的树上从1开始不回头走到某个底端点后得到所有经过的点的权值后,这些点权值修改为0,到达底部后重新回到1,继续走,问走k次,最多能得到多少权值之和 这其实就是相当于每一次都走 ...
- hdu 5242 Game(树链剖分,贪心¥)
Game Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 5242 树链剖分思想的贪心
题意及博客 树链剖分分为2步,第一次求出深度,重儿子,第二次求出重链,用到了启发式的思想,即对于比较重的儿子,尽量去完整的维护它.类似于我们去合并两个堆,明显把小的堆逐个插入大的堆中会比大的往小的插更 ...
- HDU 5242 Game (贪心)
题意:给定一棵树,要求从根结点1走k次,每次都是到叶子结点结束,把走过的所有的结点权值加起来,最大是多少. 析:先把每个结点到根结点的路径之和求出来,然后按权值从大到小排序,然后每次把路径中的权值求出 ...
随机推荐
- 深入理解计算机系统chapter7
链接:将各种代码和数据部分收集起来并组合成为单一文件的过程,这个文件可被加载到存储器并执行. 在运行时,和一个在存储器中的程序链接起来 二.静态链接库与动态链接库 静态连接库就是把(lib)文件中用到 ...
- leetCode in Java (一)
前言 感觉写博客是一个很耗心力的东西T_T,简单的写了似乎没什么用,复杂的三言两语也只能讲个大概,呸呸...怎么能有这些消极思想呢QAQ!那想来想去,先开一个leetcode的坑,虽然已经工作了 ...
- 【充分利用你的Azure】将Azure用作云计算平台(1)
本文将围绕几个步骤来讲. 因为本人是MSP,微软送了150刀的额度给我随便使用.这篇文章是要讲将Azure用作云计算平台,对于我来说,我是做机器学习的,那么Azure就要有机器学习的平台. 本文的目的 ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 移动WEB 响应式设计 @media总结
第一种: 在引用样式的时候添加 <link rel="stylesheet" media="mediatype and|not|only (media featur ...
- Spring读书笔记——bean加载
我们的日常开发几乎离不开Spring,他为我们的开发带来了很大的便捷,那么Spring框架是如何做到方便他人的呢.今天就来说说bean如何被加载加载. 我们在xml文件中写过太多类似这样的bean声明 ...
- 基于java的后台截图功能的实现
Java后台截图功能的实现 背景介绍: 在近期开发的可视化二期项目中的邮件项目中,邮件中的正文中含有图片.该图片的产生是将一些html网页转为图片格式,刚开始考虑使用第三方组件库html2image和 ...
- java程序调用存储过程和存储函数
java程序调用存储过程 jdbcUtil.java文件 package cn.itcast.oracle.utils; import java.sql.Connection; import java ...
- 图片载入状态判断及实现百分比效果loading
前言 一些大的外部资源会导致页面加载速度慢,这时候一般会加上loading效果:这里实现的是根据图片加载进度的百分比loading效果 如何判断图片加载的状态 1.onload onerror 推荐 ...
- python模拟登陆 pixiv
##---author:wuhao##在QQ群看到有群友在模拟登陆 pivix.cn 这个网站,闲来无事,我也写了一个测试一下,起初我把它想的复杂了,认为我需要获取服务器返回过来的Set-Cookie ...