Little Q is fighting against scary monsters in the game ``Monster Hunter''. The battlefield consists of nn intersections, labeled by 1,2,...,n1,2,...,n, connected by n−1n−1bidirectional roads. Little Q is now at the 11-th intersection, with XX units of health point(HP). 
There is a monster at each intersection except 11. When Little Q moves to the kk-th intersection, he must battle with the monster at the kk-th intersection. During the battle, he will lose aiai units of HP. And when he finally beats the monster, he will be awarded bibi units of HP. Note that when HP becomes negative(<0<0), the game will over, so never let this happen. There is no need to have a battle at the same intersection twice because monsters do not have extra life. 
When all monsters are cleared, Little Q will win the game. Please write a program to compute the minimum initial HP that can lead to victory. 

InputThe first line of the input contains an integer T(1≤T≤2000)T(1≤T≤2000), denoting the number of test cases. 
In each test case, there is one integer n(2≤n≤100000)n(2≤n≤100000) in the first line, denoting the number of intersections. 
For the next n−1n−1 lines, each line contains two integers ai,bi(0≤ai,bi≤109)ai,bi(0≤ai,bi≤109), describing monsters at the 2,3,...,n2,3,...,n-th intersection. 
For the next n−1n−1 lines, each line contains two integers uu and vv, denoting a bidirectional road between the uu-th intersection and the vv-th intersection. 
It is guaranteed that ∑n≤106∑n≤106. 
OutputFor each test case, print a single line containing an integer, denoting the minimum initial HP. 
Sample Input

1
4
2 6
5 4
6 2
1 2
2 3
3 4

Sample Output

3

思路:直接贴一个官方题解

代码如下:(不懂的可以再问我):
typedef long long LL;

const int maxm = ;

struct Node {
int id, change;
LL a, b;
bool operator < (const Node &x) const {
if(a >= b && x.a < x.b ) return true; // b > a 的优先
if(a < b && x.a >= x.b ) return false;
if(a < b && x.a < x.b ) return a>x.a;//a < b,按照a从小到大
if(a >= b && x.a >= x.b) return b<x.b;//a >= b,按照b从大到小
} void operator += (const Node &n) { // A means the minimum HP to kill monster
LL A = a, B = b;
if(b < n.a) {
A = a + n.a - b;
B = n.b;
} else {
B = b - n.a + n.b;
}
a = A, b = B;
}
} buf[maxm]; int fa[maxm], vis[maxm], cnt, n; vector<int> G[maxm]; void build(int u, int father) {
fa[u] = father;
for (int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v != father)
build(v, u);
}
} int Find(int u) {
if(vis[fa[u]])
return fa[u] = Find(fa[u]);
else
return fa[u];
} void init() {
cnt = ;
buf[].a = buf[].b = buf[].change = ;
buf[].id = ;
memset(vis, , sizeof(vis)), memset(fa, , sizeof(fa));
for (int i = ; i <= n; ++i)
G[i].clear();
} int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
init();
priority_queue<Node> q;
for (int i = ; i <= n; ++i) {
scanf("%lld%lld", &buf[i].a, &buf[i].b);
buf[i].id = i, buf[i].change = ;
q.push(buf[i]);
}
for (int i = ; i < n - ; ++i) {
int t1, t2;
scanf("%d%d", &t1, &t2);
G[t1].push_back(t2), G[t2].push_back(t1);
}
build(, );
while(!q.empty()) {
Node tmp = q.top();
q.pop();
int u = tmp.id;
if(vis[u] || tmp.change != buf[u].change)
continue;
vis[u] = ;
int f = Find(u);
buf[f] += buf[u];
if(f > ) {
buf[f].change = ++cnt;
q.push(buf[f]);
}
}
printf("%lld\n", buf[].a);
}
return ;
}

tmp.change != buf[u].change: 堆中有修改前的u和修改后的u

 

Day3-A-Problem H. Monster Hunter HDU6326的更多相关文章

  1. HDU 6326 Problem H Monster Hunter

    \(\mathtt{Problem H}\) \(\mathtt{Monster}\) \(\mathtt{Hunter}\) \(\mathcal{Description}\) 题目 给定一棵 \( ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. 清北学堂入学测试P4751 H’s problem(h)

    P4751 H’s problem(h)  时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 小H是一个喜欢逛街的女孩子,但是由于上了大学 ...

随机推荐

  1. DOC文档与DOCX文档有什么区别

    doc 是 Microsoft Office 2003 里的 Word 文档,而 docx 是 Microsoft Office 2007 里的 Word 文档.高版本是向下兼容的,也就是能够打开 d ...

  2. Vue 实现全局使用sass, less变量

    首先   运行这行命令 npm install sass-resources-loader --save-dev: 在项目的build/utils.js中,找到 function generateLo ...

  3. 【PAT甲级】1056 Mice and Rice (25 分)

    题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组, ...

  4. C++11常用特性介绍——array容器

    std::array是具有固定大小的数组,支持快速随机访问,不能添加或删除元素,定义于头文件<array>中. 一.概要 array是C++11新引入的容器类型,与内置数组相比,array ...

  5. 简单bat脚本

    hwf.bat: set GAP_HOME=%~dp0\.. ::copy "%JAVA_HOME%\bin\javaw.exe" "%JAVA_HOME%\bin\HW ...

  6. 基于 Chrome 浏览器的扩展插件来进行的安装Postman

    我会给你一个安装包,见附件.你应该下载下来,解压缩到你喜欢的位置. 打开 Chrome 浏览器的「扩展程序」 点击「加载已解压的扩展程序...」按钮,找到你刚刚下载的安装包的位置,点击确定. 你去看看 ...

  7. python中 yield 的用法 (简单、清晰)

    首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂 ...

  8. leetcode刷题-- 5. 动态规划

    动态规划思路 参考 状态转移方程: 明确「状态」-> 定义dp数组/函数的含义 -> 明确「选择」-> 明确 base case 试题 53最大子序和 题目描述 53 给定一个整数数 ...

  9. python包管理历史

    1.标准库工具distutils,2000年发布,是包安装和发布工具 setup.python 程序,利用distutils 开发 示例: python setup.py install 安装一个包 ...

  10. 【PAT甲级】1027 Colors in Mars (20 分)

    题意: 输入三个范围为0~168的整数,将它们从十三进制转化为十进制然后前缀#输出. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...