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. robotFramework_ride_python2_Wxpython测试环境搭建

    (提示:我的安装版本是robotFramework3.0+ride1.5+python2.7+wxpython2.8,至于wxpython3.0下ride安装打不开的问题我还没找到原因,建议刚开始先不 ...

  2. appium 中手势密码的定位坐标

    原文地址http://www.cnblogs.com/smallTestKK/p/5408143.html 1.press方法中的坐标为绝对坐标,moveTO方法中的坐标都是相对坐标,具体来说第一个m ...

  3. php 通过http user-agent判断是否为手机浏览器

    <?php/*** 判断是否是通过手机访问* @return bool 是否是移动设备    */public function isMobile() {  //判断手机发送的客户端标志  if ...

  4. 160726 smarty 笔记(2)

    <?php //取当前页 $p=1; if(!empty($_GET["page"])) { $p=$_GET["page"]; } //定义页面缓存文件 ...

  5. struts.xml 配置文件的主要元素

    1.package元素 作用: 在struts2的配置文件中引入了面向对象思想.分包管理,易于管理动作类,便于模块化开发动作类. 属性: name:包的名称.名称唯一. extends:一般情况下需要 ...

  6. C#数组的Map、Filter、Reduce操作

    在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...

  7. 【Python】闭包 & 匿名函数

    闭包 1.注意:返回的函数内部不要使用后续会发生变化的变量. def f(): gs = [] for k in range(1, 4): def g(i): return i + k gs.appe ...

  8. Python面试题之Python面向对象编程汇总

    面向对象的设计思想是从自然界中来的,因为在自然界中,类(Class)和实例(Instance)的概念是很自然的.Class是一种抽象概念,比如我们定义的Class——Student,是指学生这个概念, ...

  9. windows忘记密码

    方法一 在开机时,按下F8进入”带命令提示符的安全”模式 输入”NET USER+用户名+123456/ADD”可把某用户的密码强行设置为”123456″ 方法二 如用户忘记登入密码可按下列方法解决 ...

  10. 20145329《Java程序设计》第六周学习总结

    教材学习内容总结 第十章 InputSream与OutputStream Java中,输入串流代表对象为java.io.InputStream实例,输出串流代表对象为java.io.OutputStr ...