~欢迎你!第 AmazingCounters.com 位造访者

ACM: Long Live the Queen - 树上的DP

Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

Description

 

The Queen of Byteland is very loved by her people. In order to show her their love, the Bytelanders have decided to conquer a new country which will be named according to the queen's name. This new country contains N towns. The towns are connected by bidirectional roads and there is exactly ONE path between any two towns, walking on the country's roads. For each town, the profit it brings to the owner is known. Although the Bytelanders love their queen very much, they don't want to conquer all the N towns for her. They will be satisfied with a non-empty subset of these towns, with the following 2 properties: there exists a path from every town in the subset to every other town in the subset walking only through towns in the subset and the profit of the subset is maximum. The profit of a subset of the N towns is equal to the sum of the profits of the towns which belong to the subset. Your task is to find the maximum profit the Bytelanders may get.

 

Input

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

 

Output

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

 

Sample Input

5
-1 1 3 1 -1
4 1
1 3
1 2
4 5

Sample Output

4

Author : Mugurel Ionut Andreica
Resource : SSU::Online Contester Fall Contest #2
Date : Fall 2002
 
 
 
 
/*/
题意:
有N个村庄,每个村庄有一个权值,有n-1条路,将村庄连起来,然后选取这些路中的一个联通图,权值最大。

整个图都被联通,找其中权值最大的子联通块。

树状DP。

代码风格学了某个学长的写了个结构体,真刺激。。
AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n"
#define bigfor(x) for(LL qq=1;qq<= T ;qq++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int MX = 16666;


struct Treedp {
	struct Edge {
		int v,nxt;
	} E[MX<<1];

	int Head[MX],erear;
	bool vis[MX];
	int dp[MX];
	int INF=-1e9-1e5;

	void init() {
		erear=0;
		memset(E,0);
		memset(vis,0);
		memset(Head,-1);
	}

	void add(int u,int v) {
		E[erear].v=v;
		E[erear].nxt=Head[u];
		Head[u]=erear++;
	}

	int run(int u) {
		vis[u]=1;
		for(int i=Head[u]; ~i; i=E[i].nxt) {
			int v=E[i].v;
			if(!vis[v]) {
				dp[u]+=max(0,run(v));
			}
		}
		return dp[u];
	}

	void print(int n) {
		for(int i=1; i<=n; i++)
			cout<<dp[i]<<" ";
		puts("");
	}
};

Treedp tdp;


int main() {
	int n,l,r;
	scanf("%d",&n);
	tdp.init();
	for(int i=1; i<=n; i++) {
		scanf("%d",&tdp.dp[i]);
	}
	for(int i=1; i<n; i++) {
		scanf("%d%d",&l,&r);
		tdp.add(l,r);
		tdp.add(r,l);
	}
	int maxx=-1e9-1000000;
	tdp.run(1);
	for(int i=1; i<=n; i++) {
		maxx=max(maxx,tdp.dp[i]);
	}
//	tdp.print(n);
	printf("%d\n",maxx);
	return 0;
}

  

 
posted @ 2016-08-13 23:29  ~HDMaxfun  阅读(198)  评论(0编辑  收藏  举报