P1122 最大子樹和

來源:互聯網
上載者:User

標籤:span   line   open   stream   size   傳送門   names   ble   i++   

傳送門思路:

  任意找一個點為樹根。DFS 遍曆樹,如果子樹和為負就直接跳過,不然就統計進答案。( 雖是任意取一點為根,但不一定從這個點出發能夠取得最優解,要開一個 ans 記錄一下最大值。)

標程:
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<cstdlib>#include<stack>#include<vector>#include<queue>#include<deque>#include<map>#include<set>using namespace std;#define lck_max(a,b) ((a)>(b)?(a):(b))#define lck_min(a,b) ((a)<(b)?(a):(b))#define maxn 16002typedef long long LL;LL sum[maxn],n;LL head[maxn<<1],cnt=0;vector<LL>son[maxn];LL w[maxn],ans=-maxn;struct hh{    LL nex,to;}t[maxn<<1];inline LL read(){    LL kr=1,xs=0;    char ls;    ls=getchar();    while(!isdigit(ls))    {        if(!(ls^45))            kr=-1;        ls=getchar();    }    while(isdigit(ls))    {        xs=(xs<<1)+(xs<<3)+(ls^48);        ls=getchar();    }    return xs*kr;}LL u,v;inline void add(LL nex,LL to){    t[++cnt].nex=head[nex];    t[cnt].to=to;    head[nex]=cnt;}inline void dfs(LL u,LL fa){    for(LL i=head[u];i;i=t[i].nex)    {        LL v=t[i].to;        if(v==fa) continue;        son[u].push_back(v);        dfs(v,u);    }    if(son[u].size())    {        for(LL j=0;j<son[u].size();j++)        {            if(w[son[u][j]]<0) continue;            w[u]+=w[son[u][j]];        }    }    w[u]+=sum[u];    ans=lck_max(ans,w[u]);return ;}int main(){    //freopen("t.in","r",stdin);    n=read();    for(LL i=1;i<=n;i++)        sum[i]=read();    for(LL i=1;i<n;i++)    {        u=read();v=read();add(u,v);add(v,u);    }    dfs(1,0);    printf("%lld\n",ans);return 0;}

 

P1122 最大子樹和

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.