UVALive 6584 Escape (Regionals 2013 >> Europe - Central)
题目
给出一棵树,每个节点有一个怪物或血药,遇到怪物必须打,打完后扣掉一定的血量。
一开始英雄的血量为\(0\),当血量小于\(0\)时就挂了。
给出英雄的起点和终点,问能否成功到达终点。
算法
这题的方法真的是太美妙了!感觉ACM的题目比OI的好多了,因为它的程序不长,并且每题都不容易想啊。
我们先来解决一个弱化版问题:把起点作为根,问英雄最多能加多少血。
我们可以想象每个点有一个礼包,这个礼包有两个属性,不妨用\((a,b)\)表示,表示当英雄的血量不少于\(a\)时可以加上\(b\)点血量(\(b\)是负数就减血量)。但是有些限制,如果要取某个礼包,那么它的所有祖先上的礼包都必须先取。
我们现在尝试对每棵子树维护一些礼包,首先要保证所有的礼包都是奖励的。我们不妨假设现在对于点\(v\),要根据它的所有儿子\(u_i\)的礼包合成点\(v\)子树的礼包。对于点\(v\),如果它有怪物,我们就要想办法通过提高\(a\)使得\(b\)变为非负数(提高\(a\)是为了取子树的礼包),如果不行,那么这棵子树是不可能走进去的(因为走进去就一定扣血啊)。
通过提高\(a\)的值,现在我们成功地在点\(v\)已经生成了一个礼包,如此下去,我们对于根就生成了一棵礼包树,每个点都是有奖励的!我们就贪心(先选\(a\)最小的)往下走就好了,这个贪心的话,可以用一个左偏树来维护礼包。每开启一个礼包,就把这个礼包的儿子也加进去。
解决了这个弱化版问题后,剩下的就简单了。
代码
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long i64;
const int MAXN = (int) 2e5 + 3;
int n, target;
int A[MAXN];
struct Node{
Node* s[2];
int dis;
i64 minus, plus;
void init() {
s[0] = s[1] = NULL;
dis = 0;
minus = plus = 0;
}
};
Node node[MAXN];
struct Edge {
Edge* next;
int to;
};
Edge mem[MAXN * 2];
Edge* info[MAXN];
Edge* curMem;
void insert(int a, int b) {
curMem->to = b;
curMem->next = info[a];
info[a] = curMem ++;
}
Node* merge(Node* x, Node* y) {
if (! x) return y;
if (! y) return x;
if (x->minus > y->minus) swap(x, y);
x->s[1] = merge(x->s[1], y);
if (x->s[1] ? x->s[1]->dis : 0 > x->s[0] ? x->s[0]->dis : 0)
swap(x->s[0], x->s[1]);
x->dis = x->s[1] ? x->s[1]->dis + 1 : 1;
return x;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%*d");
while (scanf("%d%d", &n, &target) == 2) {
curMem = mem;
memset(info, 0, sizeof info);
for (int i = 1; i <= n; i ++)
scanf("%d", A + i);
for (int i = 1; i < n; i ++) {
int v, u;
scanf("%d%d", &v, &u);
insert(v, u);
insert(u, v);
}
static int que[MAXN];
static int fa[MAXN];
memset(fa, 0, sizeof fa);
int low = 0, high = 0;
que[high ++] = target;
while (low < high) {
int v = que[low ++];
for (Edge* pt = info[v]; pt; pt = pt->next) {
int u = pt->to;
if (u != fa[v]) {
fa[u] = v;
que[high ++] = u;
}
}
}
static bool path[MAXN];
memset(path, 0, sizeof path);
for (int v = 1; v != 0; v = fa[v])
path[v] = true;
for (int i = high - 1; i > 0; i --) {
int v = que[i];
if (path[v]) continue;
Node* son = NULL;
for (Edge* pt = info[v]; pt; pt = pt->next)
if (! path[pt->to] && pt->to != fa[v])
son = merge(son, &node[pt->to]);
Node* cur = node + v;
cur->init();
cur->minus = max(0, - A[v]);
cur->plus = A[v];
while (son && (cur->plus < 0 || cur->minus > son->minus)) {
if (cur->plus < 0) {
cur->minus = max(cur->minus, son->minus - cur->plus);
cur->plus += son->plus;
}
else {
cur->plus += son->plus;
}
son = merge(son->s[0], son->s[1]);
}
cur = merge(cur, son);
if (cur->plus < 0) cur = NULL;
}
i64 hp = 0;
bool ok = true;
Node* tree = NULL;
for (int v = 1; v != 0; v = fa[v]) {
hp += A[v];
if (hp < 0) {
ok = false;
break;
}
for (Edge* pt = info[v]; pt; pt = pt->next) {
int u = pt->to;
if (path[u]) continue;
if (u == fa[v]) continue;
tree = merge(tree, &node[u]);
}
while (tree && hp >= tree->minus) {
hp += tree->plus;
tree = merge(tree->s[0], tree->s[1]);
}
}
if (ok) printf("escaped\n");
else printf("trapped\n");
}
return 0;
}
UVALive 6584 Escape (Regionals 2013 >> Europe - Central)的更多相关文章
- UVALive 6931 Can't stop playing (Regionals 2014 >> Europe - Central)
题目 一开始有一个双头队列,每次添加一个数(这是数是二的幂,所有数的和不大于\(2^13\)),由你来决定添加到队头还是队尾.如果队列里面相邻的两个数相同,设它们都是\(x\),那么这两个数会合并为\ ...
- Regionals 2013 :: North America - Southeast USA
Regionals 2013 :: North America - Southeast USA It Takes a Village As a Sociologist, you are studyin ...
- UVALive 5545 Glass Beads
Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...
- UVALive 2664 One-way traffic
One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...
- UVALive 2957 Bring Them There
Bring Them There Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. O ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 5583 Dividing coins
Dividing coins Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 5292 Critical Links
Critical Links Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- 世界GDP数据可视化
各国GDP数据可视化 数据来自世界银行 导入资源包,如下: Pandas, numpy, seaborn 和 matplotlib import pandas as pd import numpy a ...
随机推荐
- 用composer安装 Laravel | Laravel需要的环境配置
一:安装 参考网站 http://v4.golaravel.com/docs/4.2/installation 1.先确保安装好了PHP,和服务器环境 2.然后下载composer可执行文件到当前目录 ...
- python中的map,filter,zip函数
map() Return an iterator that applies function to every item of iterable, yielding the results 例如: a ...
- python优秀库 - 使用envelopes发送邮件
这里有一个使用python自带lib发送邮件的例子(http://my.oschina.net/leejun2005/blog/74416),这里面讲解的很全面,可以供大家参考. 今天将的是使用env ...
- office2013破解工具
终于找到破解工具了,每次打开word文档都弹出限期激活对话框,真的是相当烦躁啊,先把破解工具奉上!!!! 工具名: HEU-KMS-ActivatorV1.1绿色版 链接: office2013 和 ...
- 监测scroll
$(window).scroll(function() { var scrollValue = $(this).scrollTop(); var h=200+scrollValue; $('.yui3 ...
- Hadoop_Lucene
http://codelife.me/blog/2012/11/03/jackson-polymorphic-deserialization/ http://itindex.net/blog/2012 ...
- EAN 通用商品条形码
商品条形码是指由一组规则排列的条.空及其对应字符组成的标识,用以表示一定的商品信息的符号.其中条为深色.空为纳色,用于条形码识读设备的扫描识读.其对应字符由一组阿拉伯数字组成,供人们直接识读或通过键盘 ...
- 一个简单的基于canvas小游戏
GDOI2016是我的退役战,不知道是题目画风不对,还是我自身的问题. 不过没关系啦,反正已经进过一次队OI生涯就没有什么遗憾的了. 这几天尝试着去做了个所谓的html5小游戏,略显简陋,但还是写个总 ...
- 一、cocos2dx之如何优化内存使用(高级篇)
本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=93 一.内存优化原则 为了优化应用内存,你应该知道是什么消耗了你应用的大部分内存,答案就是Textu ...
- sublime快捷方式和node.js
快捷方式具体见:http://blog.useasp.net/archive/2013/06/14/sublime-text-2-all-default-Shortcuts-table-on-wind ...