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. 20145236 《Java程序设计》 第十周学习总结

    20145236 <Java程序设计> 第十周学习总结 Java网络编程 Java网络编程技术 Java语言是在网络环境下诞生的,所以Java语言虽然不能说是对于网络编程的支持最好的语言, ...

  2. Hadoop 添加删除数据节点(datanode)

    前提条件: 添加机器安装jdk等,最好把环境都搞成一样,示例可做相应改动 实现目的: 在hadoop集群中添加一个新增数据节点. 1. 创建目录和用户  mkdir -p /app/hadoop gr ...

  3. 在列表页,按照指定的category取所属的post列表

    在某些指定的页面,例如news,blog等页面,需要列出指定某种类型的文章列表,这种情况下,可以先添加两个category,分别命名news,blog,然后再分别添加几个post,指定每个post所属 ...

  4. Configure,make,make install详解

    转:http://my.oschina.net/qihh/blog/66113?fromerr=6ej3CfGJ   无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件 ...

  5. js数量添加(如购物车添加,删减数量)

    <div class="fl shop-num ml24"><input type="button" style=" border- ...

  6. Future 模式介绍

    假设一个任务执行需要花费一些时间,为了省去不必要的等待时间,可以先获取一个提货单,即future,然后继续处理别的任务,知道货物到达,即任务完成得到结果,此时可以使用提货单提货,即通过future得到 ...

  7. 如何解决链入js,innerHTML中文乱码问题呢?

    描述:发生在做suhuotong网站的时候,添加在线客服代码的时候三个地方1.将js以UTF-8无BOM编码:VS修改或者使用NotePad++修改2.<meta http-equiv=&quo ...

  8. 使用AlarmManager设置闹钟----之二

    import android.media.MediaPlayer;import android.os.Bundle;import android.app.Activity;import android ...

  9. bzoj 1926: [Sdoi2010]粟粟的书架

    #include<cstdio> #include<iostream> #define N 201 #define M 500008 using namespace std; ...

  10. JavaScript编程异步助手:Promise

    异步模式在Web编程中变得越来越重要,对于Web主流语言JavaScript来说,这种模式实现起来不是很利索,为此,许多JavaScript库(比如 jQuery和Dojo.AngularJS)添加了 ...