HDU 6326 Problem H Monster Hunter
\(\mathtt{Problem H}\) \(\mathtt{Monster}\) \(\mathtt{Hunter}\)
\(\mathcal{Description}\)
给定一棵 \(n\)\((n \leq 10^6)\) 个点的树,除 \(1\) 号结点外每个结点都有一只怪兽,打败他需要先消耗 \(a_i\) 点 \(HP\),击败后可以获得 \(b_i\) 点 \(HP\),求打败所有怪兽需要的最小 \(HP\)。
\(\mathcal{Solution}\)

先不考虑父亲的限制关系,考虑最优攻击顺序。
- 对于 \(a_i < b_i\) \(a_j < b_j\) 的怪兽,那 \(a\) 小的优先。
- 对于 \(a_i \geq b_i\) \(a_j < b_j\) 的怪兽,那优先 \(a < b\) 的。
- 对于 \(a_i > b_i\) \(a_j > b_j\) 的怪兽,那 \(b\) 大的优先。
然后再来考虑父亲的限制,对于每个儿子,如果他的优先级大于他的父亲的优先级,那我们可以把他并到他的父亲节点上,假设下次的优先级最高的是兼并后的父节点,相当于先干掉了优先级更高的子节点而后干掉父节点。
\(\mathcal{Code}\)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct Node {
long long a, b;
int id;
bool operator <(const Node &x) const {
if (b > a && x.b > x.a)
return a > x.a;
if (b <= a && x.b > x.a)
return true;
if (b <= a && x.b <= x.a)
return x.b > b;
if (b >= a && x.b <= x.a)
return false;
}
} a[N];
priority_queue<Node> q;
vector <int> edge[N];
int fa1[N], fa[N], n;
inline int read() {
int x = 0, k = 1; char c = getchar();
for (; c < 48 || c > 57; c = getchar()) k ^= (c == '-');
for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);
return k ? x : -x;
}
inline long long read1() {
long long x = 0, k = 1; char c = getchar();
for (; c < 48 || c > 57; c = getchar()) k ^= (c == '-');
for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);
return k ? x : -x;
}
void dfs(int x, int fa) {
fa1[x] = fa;
int sz = edge[x].size();
for (int i = 0; i < sz; i++) {
int y = edge[x][i];
if (y == fa)
continue;
dfs(y, x);
}
}
int find(int x) {
return (fa[x] == x) ? x : (fa[x] = find(fa[x]));
}
inline Node Merge(Node a, Node b) {
return (Node)
{a.a + std::max(0ll, - a.b + b.a),
b.b + std::max(0ll, a.b - b.a)};
}
int main() {
n = read();
for (int i = 1; i <= n; i++)
fa[i] = i;
a[1] = (Node) {0, 0, 1};
for (int i = 2; i <= n; i++)
a[i] = (Node) {read1(), read1(), i};
for (int i = 1; i < n; i++) {
int x = read(), y = read();
edge[x].push_back(y);
edge[y].push_back(x);
}
dfs(1, 1);
for (int i = 2; i <= n; i++)
q.push(a[i]);
while (!q.empty()) {
Node t = q.top();
q.pop();
if (t.id == 1) continue; // root
if (a[t.id].a != t.a || a[t.id].b != t.b)
continue; // Merge
int Fa = find(fa1[t.id]);
fa[t.id] = Fa;
// Node &tt = a[Fa];
a[Fa] = Merge(a[Fa], a[t.id]);
a[Fa].id = Fa;
q.push(a[Fa]);
}
printf("%lld\n", a[1].a);
return 0;
}
HDU 6326 Problem H Monster Hunter的更多相关文章
- HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)
6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...
- HDU暑假多校第三场H.Monster Hunter
一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...
- Day3-A-Problem H. Monster Hunter HDU6326
Little Q is fighting against scary monsters in the game ``Monster Hunter''. The battlefield consists ...
- HDU 2616 Kill the monster (暴力搜索 || 终极全阵列暴力)
主题链接:HDU 2616 Kill the monster 意甲冠军:有N技能比赛HP有M怪物,技能(A,M),能伤害为A.当怪兽HP<=M时伤害为2*A. 求打死怪兽(HP<=0)用的 ...
- 实验12:Problem H: 整型数组运算符重载
Home Web Board ProblemSet Standing Status Statistics Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem H
Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...
- Gym 100531H Problem H. Hiking in the Hills 二分
Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
随机推荐
- [js测试]JavaScript Web Quiz By davidshariff
Question1 var foo = function foo() { console.log(foo === foo); }; foo(); 输出是"true",因为foo就指 ...
- 【Kickstart】2017 Round (Practice ~ G)
Practice Round Problem A Country Leader (4pt/7pt) Problem B Vote (5pt/8pt) Problem C Sherlock and Pa ...
- easyUi-datagrid 真分页 + 工具栏添加控件
1. 新建Pager.js /** * * @param {any} el 元素 */ function showDataGrid1(el) { $(el).datagrid({ title: '分 ...
- 文本处理工具——sed基础
一sed介绍 三剑客是grep,sed,awk,功能都很强大. 其中sed是Stream EDitor,流编辑器 行,编辑器的简写,它一次处理一行内容. sed的强大在于可以对文件进行修改,很适合在脚 ...
- vue登录页和主页路由配置问题
登录页和主菜单首页是同一级的,都是用一个router-view,对于home页面里还有菜单,这里边还可以再增加一个router-view,那么在配置时候就是在home的路径增加个children路径配 ...
- jmeter之--断言json响应&json path espressions的语法
一.提取所需要断言的内容: 响应数据如下:加入需要提取id为90的值 { , "name" : "python", "url" : &quo ...
- Android Studio的Gradle错误解决方法
因为喜欢尝鲜,试了一下google的Android studio,但是在创建工程时,出现了尝试连接service.gradle.org错误的问题,查了一下,在StackOverFlow找到以下答案: ...
- 2018icpc南京/gym101981 K Kangaroo Puzzle 随机化
题意: 有一个棋盘上,1是空格,0是障碍物,一开始每个空格里都有一只袋鼠,你可以命令所有袋鼠一起向上下左右一个方向走一格,一旦碰到边界或障碍物,袋鼠就不动,如果它后面有袋鼠这两个袋鼠就会挤进一个格子, ...
- MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.
mysql 批量更新记录 MySql中4种批量更新的方法最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共 ...
- ThinkPHP5使用jwt进行会话验证
以往,没有做过前后端分离的项目之前,都是服务器渲染的模板,然后用cookie和session进行账号的权限验证或者是登录状态的管理.后来接触了vue和小程序之后,在进行前后端分离的时候,就会遇到权限验 ...