\(\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. AtCoder Beginner Contest 130 F Minimum Bounding Box 三分法求极值(WA)

    题意:给n个点的起始坐标以及他们的行走方向,每一单位时间每个点往它的方向移动一单位.问最小能包围所有点的矩形. 解法:看到题目求极值,想了想好像可以用三分法求极值,虽然我也不能证明面积是个单峰函数. ...

  2. Codeforces 1197E Culture Code DP

    题意:你有n个俄罗斯套娃,已知每个套娃的容积和体积,问有多少个子集满足以下条件: 1:这个子集是一个极大子集,即不能再添加其它的套娃到这个子集里. 2:子集的套娃之间的间隙和最小. 思路1:线段树优化 ...

  3. js 数组、字符串、Json互相转换

    arr.join(): 数组转字符串 let arr = [1,2,3,4]; let str = arr.join(','); arr.split():字符串转数组 let str = '1,2,3 ...

  4. c++11引入特性

    * 支持类内初始化. class A{ vector<string> strs{"abc", "def"}; };

  5. bzoj 2013

    http://www.lydsy.com/JudgeOnline/problem.php?id=2013 最初看这个题的时候,以为神题不可做,然后去找yzjc..然后做法过于简单了(' '       ...

  6. 07-图5 Saving James Bond - Hard Version(30 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  7. MVC MVC3中 ViewBag、ViewData和TempData的使用和区别 【转】

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  8. 膜神犇 DPH

    神犇 DPH 让我写博客.但是,似乎我已经开始写了?!! I am young and naïve!!! Let us orz DPH! 上节课讲DFS,这个是我最擅长的.“暴力出奇迹”!

  9. 使用Canvas操作像素

    现代浏览器支持通过<video>元素播放视频.大多数浏览器也可以通过MediaDevices.getUserMedia() API访问摄像头.但即使这两件事结合起来,我们也无法直接访问和操 ...

  10. 《Java技术》 第二次作业

    java第二次作业 (一)学习总结 1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法.举例说明equals方法和==的区别. 在Eclips ...