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 ...
随机推荐
- runtime的概念,message send如果寻找不到相应的对象,如何进行后续处理
运行时刻是指一个程序在运行(或者在被执行)的状态.也就是说,当你打开一个程序使它在电脑上运行的时候,那个程序就是处于运行时刻.在一些编程语言中,把某些可以重用的程序或者实例打包或者重建成为“运行库”. ...
- 【Linux命令】配置ssh远程连接步骤
安装ssh: sudo apt-get update sudo apt-get install openssh-server 查看ssh服务器是否启动: sudo ps -e | grep ssh 查 ...
- myeclipse 8.6 插件安装之SVN
在这里我要说明一点,myEclipse 8.6的插件安装和之前的版本可能会有一些区别,下面是SVN插件的安装: 1.从官网下载site-1.6.13.zip文件,网址是:subclipse.tigri ...
- mysql-5.6.17-winx64 免安装 配置
[client] default_character_set=utf8 port=3306 [mysql] # 设置mysql客户端默认字符集 default_character_set=utf8 [ ...
- HTTP BIN测试
http://httpbin.org/ Tracing XML request/responses with JAX-WS: http://stackoverflow.com/questions/19 ...
- Windows Phone 8初学者开发—第9部分:Windows Phone 8模拟器概述
原文 Windows Phone 8初学者开发—第9部分:Windows Phone 8模拟器概述 第9部分:Windows Phone 8模拟器概述 原文地址: http://channel9.ms ...
- BZOJ 1015
program bzoj1015; {$inline on} ; type node=record togo,next:longint; end; var tot,n,m,d,cnt:longint; ...
- iOS中谓词的使用
Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- 设计模式(一)工厂模式Factory(创建型)
设计模式一 工厂模式Factory 在面向对象编程中, 最通常的方法是一个new操作符产生一个对象实例,new操作符就是用来构造对象实例的.可是在一些情况下, new操作符直接生成对象会带来一些问题. ...