ACM: Long Live the Queen - 树上的DP

Long Live the Queen

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;
}

  

时间: 2024-10-20 19:29:14

ACM: Long Live the Queen - 树上的DP的相关文章

Codeforces 487B. Strip(求区间最值+线段树上的dp)

B. Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right. Now Alexandra wants to split it into some p

[ACM] ZOJ 3329 One Person Game (概率DP,有环,巧妙转化)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the

[ACM] poj 2096 Collecting Bugs (概率DP,期望)

Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2026   Accepted: 971 Case Time Limit: 2000MS   Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material st

[ACM] hdu 4248 A Famous Stone Collector (DP+组合)

A Famous Stone Collector Problem Description Mr. B loves to play with colorful stones. There are n colors of stones in his collection. Two stones with the same color are indistinguishable. Mr. B would like to select some stones and arrange them in li

sgu 143 Long live the Queen 简单树形dp

// sgu 143  Long live the Queen 简单树形dp // // 题意:在树上选一个连通字图,使得节点的权值之和最大 // f[i] 表示以该节点为根的字图权值之和的最大值 // 则有 f[i] = w[i] + sigma(max(0,f[j])) i是j的父节点 // 最后在所有的f中挑个最大值就是答案.... #include <algorithm> #include <bitset> #include <cassert> #include

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

poj 5001 Walk &amp;&amp;2014 ACM/ICPC Asia Regional Anshan Online 1005(dp)

http://acm.hdu.edu.cn/showproblem.php?pid=5001 思路:dp计算出途径每个点的总概率,1-x即为所求解. dp题,先介绍下dp[i][j]为第j步走在第i个点的概率,那么dp[i][j]=dp[x1][j-1]+dp[x2][j-1]+...,x1,x2为i 的相邻节点.上一步在相邻节点这一步才能走到该点嘛. 每个点概率要一个一个的算,当算到第ii个点时(没打错,ii个点与代码对应),从起点推起,起点嘛,算是第0步吧,每个点被选中几率1.0/n,直接计

SGU 143 Long Live the Queen (树形DP)

143. Long Live the Queen time limit per test: 0.25 sec. memory limit per test: 4096 KB 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 accor

Uva 10401 Injured Queen Problem ( 计数DP)

Problem I Injured Queen Problem Input: standard input Output: standard output Time Limit: 6 seconds Memory Limit: 32 MB Chess is a two-player board game believed to have been played in India as early as the sixth century. However, in this problem we