\(\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的更多相关文章

  1. 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 号点出 ...

  2. HDU暑假多校第三场H.Monster Hunter

    一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...

  3. Day3-A-Problem H. Monster Hunter HDU6326

    Little Q is fighting against scary monsters in the game ``Monster Hunter''. The battlefield consists ...

  4. HDU 2616 Kill the monster (暴力搜索 || 终极全阵列暴力)

    主题链接:HDU 2616 Kill the monster 意甲冠军:有N技能比赛HP有M怪物,技能(A,M),能伤害为A.当怪兽HP<=M时伤害为2*A. 求打死怪兽(HP<=0)用的 ...

  5. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

  6. 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. ...

  7. Gym 100531H Problem H. Hiking in the Hills 二分

    Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...

  8. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  9. Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

    Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

随机推荐

  1. 【leetcode】421. Maximum XOR of Two Numbers in an Array

    题目如下: 解题思路:本题的难点在于O(n)的复杂度.为了减少比较的次数,我们可以采用字典树保存输入数组中所有元素的二进制的字符串.接下来就是找出每个元素的异或的最大值,把需要找最大值的元素转成二进制 ...

  2. 【CSS】position(定位)属性

    关于CSS position,来自MDN的描述: CSS position属性用于指定一个元素在文档中的定位方式.top.right.bottom.left 属性则决定了该元素的最终位置. 然后来看看 ...

  3. 牛客挑战赛33 F 淳平的形态形成场(无向图计数,EGF,多项式求逆)

    传送门: 淳平的形态形成场 题解: 把a排序后,直接统计答案恰好为a[i]并不好做,可以统计答案>a[i]的方案数,设为\(f[i]\). 即不存在一个联通块,所有的权值都<=a[i]. ...

  4. Ververica Platform-阿里巴巴全新Flink企业版揭秘

    摘要:2019云栖大会大数据 & AI专场,阿里巴巴资深技术专家王峰带来“Ververica Platform-阿里巴巴全新Flink企业版揭秘”的演讲.本文主要从Ververica由来开始谈 ...

  5. SCP-bzoj-4734

    项目编号:bzoj-4734 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 附录: #include <bits/stdc++.h> #define range(i,c,o) f ...

  6. paper 147:Deep Learning -- Face Data Augmentation(一)

    1. 在深度学习中,当数据量不够大时候,常常采用下面4中方法:  (1)人工增加训练集的大小. 通过平移, 翻转, 加噪声等方法从已有数据中创造出一批"新"的数据.也就是Data ...

  7. StringUtils 方法全集

    最近做项目需要,经常需要最字符串进行拆分等操作,经搜索和研究,发现了一篇StringUtils方法全集的文章,不错,特贴出来,以后用: 参考:http://blog.sina.com.cn/s/blo ...

  8. PHP导出大量数据到csv表

    对于做后台开发的码农来说,从excel导入数据到数据库亦或者是从数据库导出数据到excel都是很常见的操作.由于经常遇到这样的场景,也因为从数据库导出数据到表格所遇到的坑有很多,所以需要另辟途径来进行 ...

  9. 个人笔记 - MATLAB

    1.教程 2.基本知识 2.1 帮助文档设置成中文:链接1 2.2 多行注释: 链接1 2.3 MATLAB基本数据类型: 链接1  链接2 2.4 matlab中的 ndims(a).length( ...

  10. 公司-ofo:ofo

    ylbtech-公司-ofo:ofo ofo小黄车是一个无桩共享单车出行平台,缔造了“无桩单车共享”模式,致力于解决城市出行问题.用户只需在微信公众号或App扫一扫车上的二维码或直接输入对应车牌号,即 ...