Dp largest sub-title basis of the tree and the tree (Los Valley P1122)

The largest sub-tree and

Title Description

Xiao Ming full interest in mathematics, and is a studious student, always stay in the classroom after class to ask some questions to the teacher. One day he went to school in the morning ride, the road to see the old man is a pruning plants and flowers, suddenly thought of a question about pruning flowers. So day after school, Xiao Ming to raise the issue with the teacher:

A strange flower, were above even the flowers of N, N-1 Tiao total of the flower stems together, and when not pruned every flower is not isolated. Each flower has a "beautiful index", the greater the number of flower more beautiful, but also "beautiful index" is negative, indicating flower looking at all disgusting. The so-called "pruning", which means: get rid of a branch of them, so a flower became two, which one throw. After a series of "pruning", left the last one flower (possibly a). The teacher's task is to: (what can be "trimmed" are not carried out) through a series of "pruning", so that the rest of the kindred (flower) all the flowers on the flower "Beautiful index" and the largest sum.

The teacher thought for a moment, gives positive solution. Xiao Ming see the problem is easily broken, very unhappy, so he used to ask you.

Input Format

The first line of an integer N (1≤N≤16000). It represents the original flowers growing on a total of N flowers.

The second line has N integers, I, I, represent integers of beautiful flowers index.

Then N-1 lines each two integers a, b, indicating the presence of a connection of a flower and a flower b branches.

Output Format

A number that represents the maximum value after a series of "pruning" can get "Beautiful index" sum. Guarantee absolute value is not more than 2,147,483,647.


Basis linear dp title proper way of seeking the largest sub-segment and , thinking and this is almost the biggest difference is that a linear structure is a tree structure;

State transition equation is linear dp [i] = max (dp [i] + a [i], a [i]), i mean the maximum consecutive sub-segment and ending point;

Tree state transition equation is dp [i] + = max (dp [j], 0), expressed as the root node i, j is a child node of the communication block i is the maximum sum;

This question shows once again that the difference dp tree with linear, one is from top to bottom, one is from bottom to top;

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=20010;
const int M=2000100;
const LL mod=1e8-3;
int n,a[N],head[N],cnt,dp[N],ans;
struct Node{
	int to,nex;
}edge[N*2];
void add(int p,int q){
	edge[cnt].to=q;
	edge[cnt].nex=head[p];
	head[p]=cnt++;
}
void dfs(int sn,int fa){
	dp[sn]=a[sn];
	for(int i=head[sn];~i;i=edge[i].nex){
		int v=edge[i].to;
		if(v!=fa){
			dfs(v,sn);
			dp[sn]+=max(0,dp[v]);
		}
	}
	ans=max(dp[sn],ans);
}
int main(){
	memset(head,-1,sizeof(head));
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<n;i++){
		int p,q;
		scanf("%d%d",&p,&q);
		add(p,q),add(q,p);
	}
	dfs(1,0);
	cout<<ans<<endl;
	return 0;
}

Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105222529