zoukankan      html  css  js  c++  java
  • E. Tree Painting(树形换根dp)

    http://codeforces.com/contest/1187/problem/E

    分析:问得分最高,实际上就是问以哪个节点出发得到的分数最多,而呈现成代码形式就变成了换根,max其得分!!!而要利用之前算过的得分来求。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int M=2e5+5;
    vector<int>e[M];
    int son[M];
    ll ans;
    int n;
    void dfs(int u,int f){
        son[u]=1;
        for(int i=0;i<e[u].size();i++){
            int v=e[u][i];
            if(v==f)
               continue;
            dfs(v,u);
            son[u]+=son[v];
        }
        ans+=son[u];
    }
    void DFS(int u,int f,ll now){
        ans=max(ans,now);
        for(int i=0;i<e[u].size();i++){
            int v=e[u][i];
            if(v==f)
                continue;
            DFS(v,u,now+n-2*son[v]);
        }
    }
    int main(){
    
    
        scanf("%d",&n);
        for(int i=1;i<n;i++){
    
            int u,v;
            scanf("%d%d",&u,&v);
            e[u].push_back(v);
            e[v].push_back(u);
        }
        ans=0;
        dfs(1,-1);
        DFS(1,-1,ans);
        printf("%I64d
    ",ans);
    
    }
    View Code
  • 相关阅读:
    关于高精度的那些事 ~
    LOJ #10002. 喷水装置
    [HAOI2008]糖果传递
    题解 CF1404B 【Tree Tag】
    题解 CF1407E 【Egor in the Republic of Dagestan】
    唯美歌词
    CF做题总结
    CSP2020游记
    数论
    hash好题
  • 原文地址:https://www.cnblogs.com/starve/p/11166177.html
Copyright © 2011-2022 走看看