Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10388   Accepted: 5943

Description

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.

Input

Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go N – 1 lines that describe a supervisor relation tree. Each line of the tree specification has the form: 
L K 
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 
0 0 

Output

Output should contain the maximal sum of guests' ratings.

Sample Input

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Sample Output

5

Source

 
       给出一颗有根树,每个点有权值,挑选一些点使权值和达到最大,如果选择了u点,则u的儿子便不能再选了,但是儿子下面的节点还可以选。
          直接树形dp,f[i][0]表示不选i达到的最大值,f[i][1]表示选了i点能达到的最大值,贪心的选择正数的子树。
 #include<iostream>
#include<bitset>
#include<cstring>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
int fa[];
int son[];
int next[];
int f[][];
int c[];
int N,root;
void dfs(int u,int father){
f[u][]=c[u];
f[u][]=;
int s=;
for(int i=son[u];i;i=next[i]){
if(i==father) continue;
dfs(i,u);
if(f[i][]>) f[u][]+=f[i][];
f[u][]+=max(,max(f[i][],f[i][]));
}
}
int main()
{
int i,j,k;
int u,v;
while(cin>>N){
if(N==){
cin>>N;
break;
}
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(next,,sizeof(next));
for(i=;i<=N;++i) scanf("%d",c+i);
for(i=;i<N;++i){
scanf("%d%d",&u,&v);
fa[u]=v;
next[u]=son[v];
son[v]=u;
}
for(i=;i<=N;++i)
if(!fa[i]) {root=i;break;}
dfs(root,);
cout<<max(f[root][],f[root][])<<endl;
}
return ;
}

poj-2342-简单树形dp的更多相关文章

  1. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  2. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  3. [poj 2342]简单树dp

    题目链接:http://poj.org/problem?id=2342 dp[i][0/1]表示以i为根的子树,选或不选根,所能得到的最大rating和. 显然 dp[i][0]=∑max(dp[so ...

  4. POJ 2342 Label:树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6955   Accepted: 4003 ...

  5. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  6. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  7. hdu4705 Y 简单树形DP 2013多校训练第十场 J题

    题意:求一棵树中不在一条链中的三个点的对数. 转化一下,用总对数减去在一条链上的三点对数即可. 考虑经过根节点,然后可能是不同的子树中各选一个:或者是子树中选一个,然后当前节点为根的子树以外的节点选一 ...

  8. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  9. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

  10. [10.27_P2] 统计损失 (简单树形DP)

    树形DP 简单题 Description 给定一棵树,每个节点有一个值.对于一条路径,它的值为路径上所有点的值的乘积.求出树上所有路径的值的和. 注意:单个点也算一条路径. Input 第 1 行一个 ...

随机推荐

  1. Python(socket编程——2)

    import socket ''' socket.socket(socket_family,socket_type,protocal=0) socket_family 可以是 AF_UNIX 或 AF ...

  2. 高并发下hystrix熔断超时及concurrent.RejectedExecutionException: Rejected command because thread-pool queueSize is at rejection threshold问题

    我的一个项目并发200+,观察服务器的日志发现出现了大量的熔断超时偶尔会闪现出RejectedExecutionException: Rejected command because thread-p ...

  3. ZOJ - 2587 Unique Attack (判断最小割是否唯一)

    题意:判断最小割是否唯一. 分析:跑出最大流后,在残余网上从源点和汇点分别dfs一次,对访问的点都打上标记. 若还有点没有被访问到,说明最小割不唯一. https://www.cnblogs.com/ ...

  4. HDU - 4725 The Shortest Path in Nya Graph(拆点+Dijkstra)

    题意:N个点,每个点有一个层号L,相邻的两层 Li 与 Li+1 之间的距离为C.另外给出M条无向边,求从点1到点N的最短路. 分析:同一层之间的两点距离并不是0,这是一个小坑.依次把相邻两层的所有点 ...

  5. Python开篇——Python的哲学

    今天奉上Python设计哲学,宣告着自己正式开始系统的学习Python The Zen of Python, by Tim Peters Beautiful is better than ugly.E ...

  6. JS对象深入剖析

    对象概述 Objects are mutable keyed collections.  An object is a container of properties, where a propert ...

  7. iframe跨页面调用函数

    在项目中难免会遇到这样一个问题就是页面引入了IFrame并且需要父页面调用子页面函数或者子页面需要调用父页面函数.比如说:现在有两个页面parent.html和child.html.其中parent. ...

  8. Yahoo网站性能优化的34条规则

    摘自:http://blog.chinaunix.net/uid/20714478/cid-74195-list-1.html Yahoo网站性能优化的34条规则 1.尽量减少HTTP请求次数 终端用 ...

  9. 20144303石宇森 《Java程序设计》第2周学习总结

    ---恢复内容开始--- 20144303 <Java程序设计>第2周学习总结 教材学习内容总结 一.类型: 1.Java可以区分为基本类型和类类型.类类型也称作参考类型. 2.Java中 ...

  10. 如果css足够强大了,你还会用编程的方式实现去实现css已有的功能吗?

    现在css3 都出来的,但是其实我由于一些原因,有些css2中都能支持的样式,我都没有使用过.我感觉我真的有必要静下心来,去看看那些东西,看看哪些以前都被忽视掉的. 今天我主要来讲三个对于我们编程经常 ...