Codeforces Round #168 (Div. 1), problem: (B) Zero Tree, DFS

來源:互聯網
上載者:User

做法:直接DFS即可,題目要求最小的運算元其實可以理解成必須的運算元。

若 節點 x的值為a,a<0,則它的父節點必須做a次上升操作,a>0亦然,然後在看父節點經過這樣的改造或的值,重複前面的運算即可

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define LL long longusing namespace std;const int LMT=100004;LL elem[LMT],down[LMT],up[LMT];int next[LMT],all;struct line{    int u,v,next;}e[LMT<<1];void dfs(int u,int pre){    LL uu,d;    for(int x=next[u];x!=-1;x=e[x].next)    if(e[x].v!=pre)    {        uu=d=0;        dfs(e[x].v,u);        if(elem[e[x].v]>0)        {            uu=up[e[x].v];            d=down[e[x].v]+elem[e[x].v];        }        else        {            uu=up[e[x].v]-elem[e[x].v];            d=down[e[x].v];        }        down[u]=max(d,down[u]);        up[u]=max(uu,up[u]);    }    elem[u]+=up[u]-down[u];}void insert(int u,int v){    e[all].u=u;    e[all].v=v;    e[all].next=next[u];    next[u]=all++;}int main(){    memset(next,-1,sizeof(next));    int n,u,v;    scanf("%d",&n);    for(int i=1;i<n;i++)    {        scanf("%d%d",&u,&v);        insert(u,v);        insert(v,u);    }    for(int i=1;i<=n;i++)scanf("%I64d",&elem[i]);    dfs(1,0);    printf("%I64d\n",abs(elem[1])+up[1]+down[1]);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.