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----(2222)Keywords Search(trie树)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. Pinyin4Net

    .net使用的汉字转拼音库.Pinyin4Net 是直接从 Pinyin4J 翻译过来的,很多代码甚至是直接copy的. 用法与pinyin4j完全相同,具体请查阅pinyin4j文档. —— 查看更 ...

  3. python中函数使用

    1.关于函数的调用,简单小例子: >>> def fun():  print(test) >>> test='ni hao ma ?'>>> fu ...

  4. 直接取HANA数据库数据,动态QUERY

    "COPY别人的TYPE-POOLS:ADBC. DATA LV_SQL TYPE STRING. DATA LV_FROM TYPE STRING. DATA LV_WHERE TYPE ...

  5. centos 5.8 64位系统安装 mysql5.6

    mysql5.5以上的版本编译需要 cmake         1 .安装cmake wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.g ...

  6. linux 查看僵尸进程

    top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...

  7. 用户上网的基本流程图与DNS解析原理

    1.用户上网发送请求,首先确认本地的hosts中是否含有域名,有则进行ip访问,如果没有呢?看本机的display缓存中有没有访问网站的ip,有就直接去访问 那么如果本地的hosts和缓存都没有呢?这 ...

  8. 详解centos用户&组权限&添加删除用户

    1.Linux用户操作系统 Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种: 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还有组账户所谓组账户就是用户账户的集合,ce ...

  9. xcode6中如何添加pch文件

    在Xcode6之前,新建一个工程的时候,系统会帮我们自动新建一个以工程名为名字的pch (precompile header)文件,在开发过程中,可以将那些整个工程都广泛使用的头文件包含在该文件下,编 ...

  10. Program C 暴力求解

    Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...