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. hdu-----(3746)Cyclic Nacklace(kmp)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. webform工程中aspx页面为何不能调用appcode文件夹下的类(ASP.NET特殊文件夹的用法)

    App_code 只有website类型的工程才有效. App_Code 下创建的.cs文件仅仅是“内容”不是代码.你设置那个文件为“编译”就行了. 其他特殊文件夹 1. Bin文件夹 Bin文件夹包 ...

  3. JDK1.7-LinkedList循环链表优化

    最近在看jdk1.7的时候,发现LinkedList 和1.6中的变化. 首先,简单介绍一下LinkedList: LinkedList是List接口的双向链表实现.由于是链表结构,所以长度没有限制: ...

  4. java代码抓取网页邮箱

    实现思路:1.使用java.net.URL对象,绑定网络上某一个网页的地址2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象3.通过Ht ...

  5. MVC扩展ValueProvider,通过实现IValueProvider接口创建SessionValueProvider

    □ ValueProvider的大致工作原理 →通过Request.Form, Request.QueryString, Request.Files, RouteData.Values获取数据.→然后 ...

  6. struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)

    课时22 基于XML配置方式实现对action的所有方法进行校验   使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...

  7. Tomcat编码问题

    在Tomcat7中,默认URIEncoding="iso8859-1",get请求由于url会完全出现在地址栏,所以传递中文到后台会乱码,需要改成URIEncoding=" ...

  8. S1 : 函数

    一.做为值的函数 例如,假设有一个对象数组,我们想要根据某个对象属性对数组进行排序.而传递给数组sort()方法的比较函数要接收两个参数,即要比较的值.可是,我们需要一种方式来指明按照哪个属性来排序. ...

  9. POJ 1860 Currency Exchange 最短路 难度:0

    http://poj.org/problem?id=1860 #include <cstdio> //#include <queue> //#include <deque ...

  10. Sticks(poj1011/uva307)

    题目大意: 乔治有一些碎木棒,是通过将一些相等长度的原始木棒折断得到的,给出碎木棒的总数和各自的长度,求最小的可能的原始木棒的长度:(就是将一些正整数分组,每组加起来和相等,使和尽可能小) 一开始做p ...