传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1520

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16376    Accepted Submission(s): 6241

Problem 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 T 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
 
Recommend
linle   |   We have carefully selected several similar problems for you:  1561 1011 2196 1494 2242 
 
题目大意:

学校要开一个聚会。学校的教职工之间有上下级关系,为了让所有人开心,宴会组织者决定不会同时邀请一个人和他的上级(这让我想起我们昨天晚上聚餐李晔老师不来,她怕她来了我们放不开。。。。),对于每一个人,他能给聚会带来的欢乐度有一个值,问组织者该邀请哪些人能够使宴会的欢乐度达到最大值。
解题思路:
首先是DP的部分(也是很无聊的一部分):每个参与者都有两种状态,一种是参加,一种是不参加。这个状态的后续影响就是如果他参加了,他的直接上司和直接下属都不能参加。我们可以用一个二维二态的数组来描述:dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加。状态转移方程就是dp[i][1]=dp[i][1]+dp[i-1][0],dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1])。
 
本质:将一个数字三角形的操作放在了树上
 
第一道树形dp题,纪念一下,虽然对树的部分理解的还不是很透彻
 
code:

#include <iostream>
#include <stdio.h>
#include<memory>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
#define max_v 6005
struct node
{
int pa,son;
int next;
} point[max_v]; int dp[max_v][];
//dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加
//状态转移方程就:
//dp[i][1]=dp[i][1]+dp[i-1][0]
//dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1]) int List[max_v]; int vis[max_v];//vis[a]=1 表示a有父节点 int value[max_v];//存值
int pos; void add(int pa,int son)
{
point[pos].pa=pa;
point[pos].son=son;
point[pos].next=List[pa];
List[pa]=pos++;
} void dfs(int root)
{
if(List[root]==-)//root没有子节点了
{
dp[root][]=value[root];
dp[root][]=;
return ;
} int now=List[root];
dp[root][]=;
dp[root][]=value[root]; while(now!=-)
{
dfs(point[now].son);
dp[root][]+=dp[point[now].son][];//既然取了父节点的值,子节点的值就不能再取了。 //父节点的值没有取,子节点的值分取和不取两种情况,取其中较大的那种情况。
dp[root][]+=max(dp[point[now].son][],dp[point[now].son][]); now=point[now].next;//这个子节点计算过了,就要开始计算下一个子节点了
}
return ;
} int main()
{
int n;
while(cin>>n)
{
for(int i=; i<=n; i++)
cin>>value[i];//记录每一个点的值 memset(List,-,sizeof(List));
memset(vis,,sizeof(vis)); int a,b;
pos=; while(~scanf("%d %d",&a,&b))
{
if(a==&&b==)
break;
add(b,a); //将边加入树中
vis[a]=; //记录a有父节点,不可能是祖节点。
} a=;
while(vis[a]==)
a++;//找到根结点 dfs(a);//从根结点开始搜 printf("%d\n",max(dp[a][],dp[a][]));//取最大
}
return ;
}
 
 

hdu 1520 Anniversary party(第一道树形dp)的更多相关文章

  1. TTTTTTTTTTT hdu 1520 Anniversary party 生日party 树形dp第一题

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

  2. HDU 1520.Anniversary party 基础的树形dp

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

  3. hdu 1520 Anniversary party || codevs 1380 树形dp

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

  4. hdu1520 第一道树形DP,激动哇咔咔!

    A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. hdu 1561 The more, The Better(树形dp,基础)

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

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

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

  8. HDU 1520 Anniversary party [树形DP]

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

  9. HDU 1520 Anniversary party(DFS或树形DP)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

随机推荐

  1. 在CentOS 7下更改yum源与更新系统

    在CentOS 7下更改yum源与更新系统. [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo cp /etc/yum.repos.d/CentOS-Base.rep ...

  2. python简单的爬虫

    def baidu_tieba(url,begin_page,end_page): for i in range(begin_page, end_page+1): sName = string.zfi ...

  3. sass(@at-root与&配合使用、without和with)

    @at-root与&配合使用(找父级) scss.style css.style 应用于@keyframe scss.style css.style @at-root (without: .. ...

  4. 配合sublime使用flexible.js实现微信开发页面自适应

    什么是flexible.js 是一个终端设备适配的解决方案.也就是说它可以让你在不同的终端设备中实现页面适配. 是一个用来适配移动端的javascript框架.根据宽度的不同设置不同的字体大小,样式间 ...

  5. js Array数组对象常见方法总结

    Array对象一般用来存储数据. 其常用的方法包括: 1.concat()方法 concat() 方法用于合并两个或多个数组.它不会更改现有数组,而是返回一个新数组. 例如: var arr1=[1, ...

  6. 【Python】卸载完Python3 之后 Python2 无法打开IDLE

    安装官方的Python带Idle但是却无法打开,百度谷歌了几种解决方法,加上自己的实际境况予以解决. 我的python是直接安装在C盘下的. 1.首先是设置环境变量: Path=C:\Python27 ...

  7. day14 HTML CSS

    HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏 ...

  8. DELPHI SOKET 编程--使用TServerSocket和TClientSocket

    本文采用delphi7+TServerSocket+TClientSocket; 笔者在工作中遇到对局域网中各工作站与服务器之间进行Socket通信的问题.现在将本人总结出来的TServerSocke ...

  9. 基于按annotation的hibernate主键生成策略

    基于按annotation的hibernate主键生成策略 博客分类: Hibernate HibernateJavaJPAOracleMySQL  这里讨论代理主键,业务主键(比如说复合键等)这里不 ...

  10. webpack之傻瓜式教程及前端自动化入门

    原文地址:https://www.cnblogs.com/liqiyuan/p/6246870.html 接触webpack也有挺长一段时间了,公司的项目也是一直用着webpack在打包处理,但前几天 ...