• HDU 6662 Acesrc and Travel 换根DP,宇宙最傻记录


    #include<bits/stdc++.h>
    typedef long long ll;
    using namespace std;
    const int maxn=1e6+50;
    const ll mod=1e9+7;
    
    int a[maxn],b[maxn];
    vector<int>G[maxn];
    ll dp[maxn][2]; /// 0 zhang 1 liu
    ll hello[maxn][2];
    ll hello2[maxn][2];
    int who1[maxn][2];
    int who2[maxn][2];
    
    void go1(int u,int v){
        who1[u][1]=who1[u][0];
        who1[u][0]=v;
    }
    void go2(int u,int v){
        who2[u][1]=who2[u][0];
        who2[u][0]=v;
    }
    
    ll flag[maxn];
    int n;
    void dfs1(int u,int f){
        dp[u][0]=a[u]-b[u];
        dp[u][1]=b[u]-a[u];
        for(auto i:G[u]){
            if(i==f)continue;
            dfs1(i,u);
            int v=i;
            if(dp[v][0]>dp[who1[u][0]][0]){
                go1(u,v);
            }
            else if(dp[v][0]>dp[who1[u][1]][0]){
                who1[u][1]=v;
            }
            if(dp[v][1]>dp[who2[u][0]][1]){
                go2(u,v);
            }
            else if(dp[v][1]>dp[who2[u][1]][1]){
                who2[u][1]=v;
            }
        }
        if(who1[u][0]!=0){
            dp[u][0]-=dp[who2[u][0]][1];
            dp[u][1]-=dp[who1[u][0]][0];
        }
       /*
       cout<<"dp["<<u<<"]["<<0<<"]="<<dp[u][0]<<endl;
        cout<<"dp["<<u<<"]["<<1<<"]="<<dp[u][1]<<endl;
        for(int i=1;i<=2;i++){
            for(int j=0;j<=1;j++){
                if(i==1)cout<<"who1["<<u<<"]["<<j<<"]="<<who1[u][j]<<endl;
                else cout<<"who2["<<u<<"]["<<j<<"]="<<who2[u][j]<<endl;
            }
        }
        */
    }
    ll sum[maxn];
    int tmp1[maxn][2];
    int tmp2[maxn][2];
    int tmp3[maxn][2];
    int tmp4[maxn][2];
    ll aha1[maxn][2];
    ll aha2[maxn][2];
    
    void dfs2(int u,int f){
        sum[u]=dp[u][0];
       // cout<<"dp["<<u<<"]["<<0<<"]="<<dp[u][0]<<endl;
        for(auto t:G[u]){
            if(t==f)continue;
            int v=t;
    
            for(int i=0;i<=1;i++){
                aha1[u][i]=dp[u][i];
                aha2[u][i]=dp[v][i];
            }
    
            dp[u][0]+=dp[who2[u][0]][1];
            dp[u][1]+=dp[who1[u][0]][0];
    
    
            for(int j=0;j<=1;j++){
                tmp1[u][j]=who1[u][j];
                tmp2[u][j]=who2[u][j];
                tmp3[u][j]=who1[v][j];
                tmp4[u][j]=who2[v][j];
            }
    
    
            if(who1[u][0]==v){
                who1[u][0]=who1[u][1];
            }
            if(who2[u][0]==v){
                who2[u][0]=who2[u][1];
            }
            if(who1[u][0]!=0){
                dp[u][0]-=dp[who2[u][0]][1];
                dp[u][1]-=dp[who1[u][0]][0];
            }
    
            if(who1[v][0]!=0){
                dp[v][0]+=dp[who2[v][0]][1];
                dp[v][1]+=dp[who1[v][0]][0];
            }
    
            if(dp[u][0]>dp[who1[v][0]][0]){
                go1(v,u);
            }
            else if(dp[u][0]>dp[who1[v][1]][0]){
                who1[v][1]=u;
            }
            if(dp[u][1]>dp[who2[v][0]][1]){
                go2(v,u);
            }
            else if(dp[u][1]>dp[who2[v][1]][1]){
                who2[v][1]=u;
            }
    
            if(who1[v][0]!=0){
                dp[v][0]-=dp[who2[v][0]][1];
                dp[v][1]-=dp[who1[v][0]][0];
            }
    
          //  cout<<"dp["<<u<<"]["<<0<<"]="<<dp[u][0]<<endl;
          //  cout<<"dp["<<u<<"]["<<1<<"]="<<dp[u][1]<<endl;
         /*   for(int i=1;i<=2;i++){
                for(int j=0;j<=1;j++){
                    if(i==1)cout<<"who1["<<u<<"]["<<j<<"]="<<who1[u][j]<<endl;
                    else cout<<"who2["<<u<<"]["<<j<<"]="<<who2[u][j]<<endl;
                }
            }*/
    
           /* cout<<"***************"<<endl;
            cout<<"dp["<<v<<"]["<<0<<"]="<<dp[v][0]<<endl;
            cout<<"dp["<<v<<"]["<<1<<"]="<<dp[v][1]<<endl;*/
            /*for(int i=1;i<=2;i++){
                for(int j=0;j<=1;j++){
                    if(i==1)cout<<"who1["<<v<<"]["<<j<<"]="<<who1[v][j]<<endl;
                    else cout<<"who2["<<v<<"]["<<j<<"]="<<who2[v][j]<<endl;
                }
            }
    
            cout<<"nnnnnnnnnnnnnnn"<<endl;*/
            dfs2(v,u);
    
    
            dp[v][0]+=dp[who2[v][0]][1];
            dp[v][1]+=dp[who1[v][0]][0];
    
            for(int j=0;j<=1;j++){
                who1[u][j]=tmp1[u][j];
                who2[u][j]=tmp2[u][j];
                who1[v][j]=tmp3[u][j];
                who2[v][j]=tmp4[u][j];
            }
            if(who1[v][0]!=0){
                dp[v][0]-=dp[who2[v][0]][1];
                dp[v][1]-=dp[who1[v][0]][0];
            }
            dp[u][0]-=dp[who2[u][0]][1];
            dp[u][1]-=dp[who1[u][0]][0];
    /*
            for(int j=1;j<=2;j++){
            for(int k=0;k<=1;k++){
                if(j==1){
                    cout<<"who1["<<u<<"]["<<k<<"]="<<who1[u][k]<<endl;
                }
                else{
                    cout<<"who1["<<u<<"]["<<k<<"]="<<who2[u][k]<<endl;
                }
            }
           }*/
            for(int i=0;i<=1;i++){
                dp[u][i]=aha1[u][i];
                dp[v][i]=aha2[u][i];
            }
        }
    }
    
    int main(){
        std::ios::sync_with_stdio(false);
        int t;
        cin>>t;
        dp[0][0]=-1e18;
        dp[0][1]=-1e18;
        while(t--){
            cin>>n;
            for(int i=1;i<=n;i++){
                cin>>a[i];
                G[i].clear();
                dp[i][0]=dp[i][1]=0;
                sum[i]=0;
                hello[i][0]=hello[i][1]=0;
                who1[i][0]=who1[i][1]=0;
                who2[i][0]=who2[i][1]=0;
            }
            for(int i=1;i<=n;i++)cin>>b[i];
            for(int i=1;i<n;i++){
                int u,v;cin>>u>>v;
                G[u].push_back(v);
                G[v].push_back(u);
            }
    
            dfs1(1,0);
           // cout<<"**************"<<endl;
            dfs2(1,0);
            ll ans=0;
            ll mx=-1e18;
            for(int i=1;i<=n;i++){
                mx=max(sum[i],mx);
            }
            cout<<mx<<endl;
        }
        return 0;
    }
    /*
    5
    5
    1 2 4 100 1000
    5 3 7 0 0
    1 2
    1 3
    3 4
    3 5
    
    */
    
    
  • 相关阅读:
    Visual Basic 9.0 前沿播报·静态篇(二)对象初始化器和匿名类型
    Visual Basic 9.0 前沿播报内容概览
    Refactor! for VB —— VB2005的重构支持(一)
    我不知道该说什么,这里太先进了!
    Visual Basic 9.0 前沿播报·静态篇(一)局部变量类型推测和数组初始化器
    关于“就地颠倒句子里的词”面试题
    Visual Basic 9.0 前沿播报·静态篇(三)扩展方法
    《窝窝世界》视频20101025
    Silverlight 游戏开发小技巧:透明背景的Silverlight程序
    Silverlight 游戏开发小技巧:技能冷却效果2(Cooldown)2
  • 原文地址:https://www.cnblogs.com/luowentao/p/11353842.html
Copyright © 2020-2023  润新知