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 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 2 integer 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

题意 :给定一棵树,每个节点上权值为整数。求它的一个连通子图,使其所有节点权值之和最大。

思路 :dp[ i ] 表示以编号 i 的节点为根的字数中,在选 i 这个点的前提下,可以选择的最大子树的权值和。

即   dp [ i ] += max(0,dp[ v ] )  v 为节点 i的儿子节点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int inf=999999999;
const int maxn=16010;

int dp[maxn],a[maxn],n,root;
vector <int> G[maxn];

void input()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)  scanf("%d",&a[i]);
    int u,v;
    for(int i=1; i<n; i++)
    {
        scanf("%d %d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
}

void dfs(int u,int v)
{
    for(int i=0; i<G[v].size(); i++)
    {
        int vv=G[v][i];
        if(vv!=u)  dfs(v,vv);
    }
    for(int i=0; i<G[v].size(); i++)
    {
        int vv=G[v][i];
        if(vv!=u) dp[v]+=max(0,dp[vv]);
    }
}

void solve()
{
    for(int i=1; i<=n; i++) dp[i]=a[i];
    dfs(-1,1);
    int ans=-inf;
    for(int i=1; i<=n; i++)  ans=max(ans,dp[i]);
    printf("%d\n",ans);
}

int main()
{
    input();
    solve();
    return 0;
}
时间: 2024-08-05 09:16:27

SGU 143 Long Live the Queen (树形DP)的相关文章

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

SGU 143 Long Live the Queen

题意: 输入:n代表n个城市,然后是n个数值,代表每个城市的盈利,接下来的n-1行输入a,b,代表城市ab间有一条路: 输出:连接几个城市所能达到的最大利润(每个城市间都能通) 本质上就是给出一棵无向树树和权值,求一个联通块的最大权值,典型的树形dp 1 #include <iostream> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 #define inf 0x3f3f3

树形DP求树的重心 --SGU 134

令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心的属性值. 考虑用树形DP. dp[u]表示割去u点,得到的连通分支的节点数的最大值. tot[u]记录以u为根的这棵子树的节点数总和(包括根). 则用一次dfs即可预处理出这两个数组.再枚举每个点,每个点的属性值其实为max(dp[u],n-tot[u]),因为有可能最大的连通分支在u的父亲及以上

SGU 149. Computer Network( 树形dp )

题目大意:给N个点,求每个点的与其他点距离最大值 很经典的树形dp...很久前就想写来着...看了陈老师的code才会的...mx[x][0], mx[x][1]分别表示x点子树里最长的2个距离, dfs一遍得到. mx[x][2]表示从x的父亲到x的最长路径长度, 也是dfs一遍得到(具体看代码).最后答案就是max(mx[x][0], mx[x][2]). 时间复杂度O(N) ----------------------------------------------------------

【BZOJ2286】消耗战(虚树,DFS序,树形DP)

题意:一棵N个点的树上有若干个关键点,每条边有一个边权,现在要将这些关键点到1的路径全部切断,切断一条边的代价就是边权. 共有M组询问,每组询问有k[i]个关键点,对于每组询问求出完成任务的最小代价. 对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1 思路:第一题虚树,需要详细地记录一下. 对于此题,朴素的树形DP很好理解: dp[u]为将u子树中的关键点全部切断的最小代价 dp[u]=min(cut[u]

POJ 3162 Walking Race 树形DP+线段树

给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成为一段长度为n的数列 现在mm想要从这数列中选出一个连续的区间,要求这个区间的max-min<=m 输出最长的区间 做了一个下午 思路: 分成2个部分: 1.求出数列,即对于一棵树,求出每一个节点能到达的最远距离 2.对于这段数列,选出一个区间,使得区间的max-min<=m,并且使得区间长度尽量

URAL 1018 Binary Apple Tree 树形DP 好题 经典

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu

bzoj 1017[JSOI2008]魔兽地图DotR - 树形dp

1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec  Memory Limit: 162 MB Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars.DotR里面的英雄只有一个属性--力量.他们需要购买装备来提升自己的力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄

HDU-2196 Computer (树形DP)

最近在看树形DP,这题应该是树形DP的经典题了,写完以后还是有点感觉的.之后看了discuss可以用树分治来做,以后再试一试. 题目大意 找到带权树上离每个点的最远点.︿( ̄︶ ̄)︿ 题解: 对于每一个点的最远点,就是以这个点为根到所有叶子节点的最长距离.但是如果确定根的话,除了根节点外,只能找到每个节点(度数-1)个子树的最大值,剩下一个子树是该节点当前的父亲节点. 所以当前节点的最远点在当前节点子树的所有叶子节点以及父亲节点的最远点上(当父亲节点的最远点不在当前节点的子树上时), 如果父亲节