• [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
    ",A[1].a);
    }
    
    int main(){for(T=read();T--;){Init();solve();}}
    
  • 相关阅读:
    docker常用命令
    Jenkins 插件开发记录
    【转】python作用域
    git备忘录
    【笔记】script.sh: source: not found in docker 问题
    (转)JavaScript判断浏览器类型及版本
    (转)webstorm快捷键
    (转)javaScript call 函数的用法说明
    (转载)记录函数 getStyle() 获取元素 CSS 样式
    (转)resize扩展
  • 原文地址:https://www.cnblogs.com/void-f/p/9397879.html
Copyright © 2020-2023  润新知