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(异常处理)

    一 错误和异常 程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) 2.逻辑错误(逻辑错误) 什么是异常 异常就是程序运行时发 ...

  2. mysql update 多表 (复制)

    定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是ProductPrice表,我们要将ProductPrice表中的价格字段Price更新为Price表中价 ...

  3. discuz debug下载地址

      Discuz!X会自带Debug模式,主要是用于排查性能问题.垃圾站建议当如果您的站点调试完之后,一定要把debug模式关闭,以免其他用户会获取到一些敏感信息.在调试过程中,config文件配置的 ...

  4. java并发 —— Lock

    java并发 -- Lock 关于java并发中的锁知识,少不了 Lock.本文转载自:Java并发编程:Lock. 从Java 5之后,在java.util.concurrent.locks包下提供 ...

  5. vue-cli中的.babelrc文件介绍

    转载自:http://www.cnblogs.com/ye-hcj/p/7071850.html { // 此项指明,转码的规则 "presets": [ //个人认为多此一举 [ ...

  6. openwrt下定义软件包的依赖关系类型

    在openwrt下软件包的依赖关系由DEPENDS:=来指定 第一种依赖关系类型为只有将依赖的软件包手动选上,当前的软件包就会自动被选中,用法为DEPENDS:=package_name 第二种依赖关 ...

  7. oracle的 分表 详解 -----表分区

    此文从以下几个方面来整理关于分区表的概念及操作:         1.表空间及分区表的概念         2.表分区的具体作用         3.表分区的优缺点         4.表分区的几种类 ...

  8. ipvs+ldirectord实现高可用ipvs

    一.heartbeat准备 1.接上文 2. 安装heartbeat-ldirectord组件包 [root@node1 heartbeat]# -.el6.x86_64.rpm 3.ldirecto ...

  9. String中的equals方法解析 jdk1.7

    注  此篇为jdk1.7中的源码解析 equals()方法中的判断分一下步骤 1先判断内存地址是否相同  如果内存地址相同 那么字符串就是相同的 返回true 2 判断当前字符串和参数字是否属于同一类 ...

  10. Nginx+vsftpd搭建图片服务器

    安装Nginx 参考:http://www.cnblogs.com/idefav2010/p/nginx-concat.html Nginx配置文件 location ~ .*\.(gif|jpg|j ...