SGU P143 Long Live the Queen【樹形DP】

來源:互聯網
上載者:User

題目索引:http://acm.sgu.ru/problem.php?contest=0&problem=143

143. Long Live the Queen

time limit per test: 0.25 sec.
memory limit per test: 4096 KB

The Queen of Bytelandis very loved by her people. In order to show her their love, the Bytelandershave decided to conquer a new country which will be named according tothe queen's name. This new country contains N towns. The towns areconnected by bidirectional roads and there is exactly ONE path betweenany two towns, walking on the country's roads. For each town, the profitit brings to the owner is known. Although the Bytelanders love their queenvery much, they don't want to conquer all the N towns for her. Theywill be satisfied with a non-empty subset of these towns, with the following2properties: there exists a path from every town in the subset to everyother town in the subset walking only through towns in the subset and theprofit of the subset is maximum. The profit of a subset of theNtowns is equal to the sum of the profits of the towns which belong to thesubset. Your task is to find the maximum profit the Bytelanders may get.

Input

The first line ofinput will contain the number of towns N (1<=N<=16 000). Thesecond line will contain N integers: the profits for each town,from 1 to N. Each profit is an integer number between -1000and 1000. The next N-1 lines describe the roads: each linecontains 2 integer numbers a and b, separated by blanks,denoting two different towns between which there exists a road.

Output

The output shouldcontain one integer number: the maximum profit the Bytelanders may get.

Sample Input

5-1 1 3 1 -14 11 31 24 5

Sample Output

4

題目翻譯:

       給定一棵每個點都一個整數權值的樹。求它的一個連通子圖,使得該連通子圖的節點權值之和最大。求該最大值。

題目分析:

       這是一道很顯然並且簡單的樹形DP。

       於是我們用F[I],表示以編號為I的節點為根的子樹中,在選I點的前提下,可以得到的最大子樹的權值和,Fa[I]表示I的父親節點。於是我們仍然可以簡單得出狀態轉移方程:F[I]=max{0,V[I]+sigma(0,F[J])(Fa[J]=I)}。那麼我們應該如何理解這個方程呢。這樣來想:如果某個節點為根的子樹最大權值<0,那麼這整棵子樹都不應該加入F[I]。最後答案即在F[1],F[2],F[3],...,F[N]取最大即可。

       代碼如下:

      

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define DB double#define SG string#define LL long long#define Fp(A,B,C,D) for(A=B;A<=C;A+=D)#define Fm(A,B,C,D) for(A=B;A>=C;A-=D)#define Clear(A) memset(A,0,sizeof(A))using namespace std;const LL Max=2e4+5;const LL Mod=1e9+7;const LL Inf=1e18;LL N,M,Ans=-Inf,V[Max],DP[Max];LL Cnt,To[Max<<1],Next[Max<<1],Edge[Max<<1],Head[Max];inline LL Read(){LL X=0;char CH=getchar();bool F=0;while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}return F?-X:X;}inline void Write(LL X){if(X<0)X=-X,putchar('-');if(X>9)Write(X/10);putchar(X%10+48);}void Insert(LL X,LL Y,LL Z){To[++Cnt]=Y;Edge[Cnt]=Z;Next[Cnt]=Head[X];Head[X]=Cnt;}LL Tree_DP(LL X,LL Fa){LL I,J,K;DP[X]=V[X];for(I=Head[X];I;I=Next[I]){LL Y=To[I],Tmp=0;if(Y==Fa){continue;} else {Tmp=Tree_DP(Y,X);if(Tmp>0){DP[X]+=Tmp;}}}return DP[X];}int main(){LL I,J,K;N=Read();Fp(I,1,N,1){V[I]=Read();}Fp(I,1,N-1,1){LL X=Read(),Y=Read();Insert(X,Y,0);Insert(Y,X,0);}Tree_DP(1,-1);Fp(I,1,N,1){Ans=max(Ans,DP[I]);}Write(Ans);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.