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

【题意】有n个人,分别给出了每个人的价值,他们要被邀请去参加聚会,但是每个人不能和自己的直接领导同时被邀请,问邀请后实现的最大价值是多少

【思路】dp[k][1]+=dp[i][0]表示k是i的领导,1表示去,0表示不去,领导k去了,i就不去了;

dp[k][0]+=max(dp[i][0],dp[i][1]);领导k不去,i可去可不去,取大值

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int n;
int fa[N],vis[N],dp[N][];
void dfs(int k)
{
vis[k]=;
for(int i=; i<=n; i++)
{
if(!vis[i]&&fa[i]==k)
{
dfs(i);
dp[k][]+=dp[i][];
dp[k][]+=max(dp[i][],dp[i][]);
}
}
}
int main()
{
while(~scanf("%d",&n))
{ memset(fa,,sizeof(fa));
for(int i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
int k=;
int a,b;
while(scanf("%d%d",&a,&b))
{ if(a==&&b==) break;
fa[a]=b;
k=b;
}
memset(vis,,sizeof(vis));
dfs(k);
printf("%d\n",max(dp[k][],dp[k][]));
}
return ;
}

Anniversary party_树形DP的更多相关文章

  1. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  2. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  3. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  4. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  6. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  7. hdu_Anniversary party_(树形DP入门题)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:有N个人,N-1个人有自己的上司,每个人有一个快乐值,如果这个人参加了聚会,那么这个人的直 ...

  8. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

  9. [poj2342]Anniversary party树形dp入门

    题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...

随机推荐

  1. javaSE之如何将一个文件复制到另一个文件

    /* * (1). 文件字符输入,输出流 * 文件字节输入,输出流的read和write方法使用 * 字节数组读写数据,即以字节为单位处理数据,因此,字节流不能很好的操作Unicode字符 * ,比如 ...

  2. 在唯一密钥属性“name”设置为“ScriptHandlerFactory”时,无法添加类型为“add”的重复集合项

    出错原因:同一根目录下有两个相同的WebConfig文件 解决办法:删除子其中一个webConfig文件. 详细:

  3. uva 10723

      10723 - Cyborg Genes Time limit: 3.000 seconds Problem F Cyborg Genes Time Limit 1 Second Septembe ...

  4. C#get,set

    一直对get,set的理解只在文字上:get 属性访问器用于返回属性值,而 set 访问器用于分配新值.其实这样理解是有点狭隘的,尤其是对set.set应该可以理解为为成员分配新值时的处理,比如一个类 ...

  5. SSL证书请求文件(CSR)生成指南 - Tomcat

    SSL证书请求文件(CSR)生成指南 - Tomcat http://www.zhenssl.com/support/CSRgen/tomcat_CSR.htm   重要注意事项 An Importa ...

  6. CSS3学习教程:Media Queries详解

    说起CSS3的新特性,就不得不提到 Media Queries . Media Queries 的引入,其作用就是允许添加表达式用以确定媒体的情况,以此来应用不同的样式表.换句话说,其允许我们在不改变 ...

  7. spring mvc重定向页面

    @RequestMapping(value="/del/{id}") public String delUser(@PathVariable int id){ return &qu ...

  8. spring mvc环境配置

    spring mvc将所有的请求都经过一个servlet控制器-DispatcherServlet,这个servlet的工作就是将一个客户端的request请求分发给不同的springmvc控制器,既 ...

  9. java 面向对象编程--第17章 I/O系统

    1.I/O操作指的是输入和输出流的操作.相对内存而言,当我们从数据源中将数据读取到内存中,就是输入流,也叫读取流.当我们将内存中处理好的数据写入数据源,就是输出流,也叫写入流. 2.流按照内容分类:字 ...

  10. 二模 (6) day2

    第一题: 题目大意:50*50的格子里玩贪吃蛇.给出N步扭头的操作,判断贪吃蛇会在第几步挂掉.(蛇初始向东) 解题过程: 1.一开始的方法是:为了加快速度,只保存头和尾的坐标,然后保存尾巴的方向,每次 ...