[HDU6326]Monster Hunter(贪心)

用(a,b)表示一个点先失去a点HP,然后增加b点HP

首先容易证明忽略父亲条件下,任意两个点,先吃b大的最优

对于一个节点v和它的父节点u,若此时选v最优,那么就是吃到u时可以立即吃掉v,

于是可以将u和v合并表示吃掉u时立即吃v,然后v的子节点连到u上

于是用一个堆维护节点,并查集找父亲

Code

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define ll long long
#define N 100010
#define mst(a) memset(a,0,sizeof(a))
#define id second.second
#define p second.first
#define PI pair<int,int>
#define PII pair<info,PI>
using namespace std;

struct edge{int to,nex;}e[N<<1];
struct info{
	ll a,b;
	info(){}
	info(ll _a,ll _b):a(_a),b(_b){}
	void operator+=(const info&o){
    	ll na=max(a,a-b+o.a),nb=b+o.b-a-o.a+na;
    	a=na,b=nb;
  	}
  	bool operator<(const info&o)const{
	    int x=a<b,y=o.a<o.b;
	    if(x!=y)return x<y;
	    if(a<b)return a>o.a;
	    return b<o.b;
  	}
}A[N];
int T,tot,head[N],n,f[N],act[N],cnt;
bool del[N];
priority_queue<PII> q;

inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void Link(int u,int v){e[++tot].to=v;e[tot].nex=head[u];head[u]=tot;}
void dfs(int u,int fa){for(int i=head[u];i;i=e[i].nex)if(e[i].to!=fa)f[e[i].to]=u,dfs(e[i].to,u);}
int Find(int x){return del[f[x]]?f[x]=Find(f[x]):f[x];}
void Init(){	
	mst(head),tot=cnt=0,mst(del),mst(f),mst(act);
	n=read(),A[1]=info(0,0);
	for(int i=2;i<=n;++i)A[i].a=read(),A[i].b=read();
	for(int i=1;i<n;++i){
		int u=read(),v=read();
		Link(u,v),Link(v,u);
	}
	dfs(1,0);
}

void solve(){
	for(int i=2;i<=n;++i)q.push(PII(A[i],PI(0,i)));
	for(;!q.empty();){
		PII u=q.top();q.pop();
		if(del[u.id]||act[u.id]!=u.p)continue;
		del[u.id]=1;
		int fa=Find(u.id);
		A[fa]+=A[u.id];
		if(fa!=1)q.push(PII(A[fa],PI(act[fa]=++cnt,fa)));
	}
	printf("%lld\n",A[1].a);
}

int main(){for(T=read();T--;){Init();solve();}}

 

转载于:https://www.cnblogs.com/void-f/p/9397879.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值