Anniversary party

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

Total Submission(s): 4310    Accepted Submission(s): 1976

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都解决出来,然后加上去就可以。

用dp[i][0]表示不选中i号,周围的都能够选的最大值,dp[i][1]表示选中i号,那么周围都不能选的最大值。

dp[i][0]+=(dp[j][1],dp[j][0])

dp[i][1]+=dp[j][0]

i和j相邻,而且在求i的时候从j扩展的都已经求出来。


详见代码:


AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int maxn=6005; vector<int> mq[maxn];
int dp[maxn][2]; void dfs(int cur,int p)
{
int i,nex;
for(i=0;i<mq[cur].size();i++)
{
nex=mq[cur][i];
if(nex==p) continue; dfs(nex,cur); //先找从cur这个点能够扩展的dp
dp[cur][0]+=max(dp[nex][0],dp[nex][1]);
dp[cur][1]+=dp[nex][0];
}
} int main()
{
int n,i;
int a,b; while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
mq[i].clear();
scanf("%d",&dp[i][1]);
} while(scanf("%d%d",&a,&b)&&(a+b))
{
mq[a].push_back(b);
mq[b].push_back(a);
} dfs(1,0); int ans=max(dp[1][0],dp[1][1]);
printf("%d\n",ans);
} return 0;
}

hdu 1520Anniversary party(简单树形dp)的更多相关文章

  1. HDU 1520-Anniversary party(树形dp入门)

    题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1 ...

  2. HDU 4714 Tree2cycle (树形DP)

    Tree2cycle Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Tot ...

  3. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  4. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  5. hdu 1054 Strategic Game (简单树形DP)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

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

  7. HDU 1520 Anniversary party [树形DP]

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

  8. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  9. HDU 1054 Strategic Game (树形dp)

    题目链接 题意: 给一颗树,用最少的点覆盖整棵树. 每一个结点可以防守相邻的一个边,求最少的点防守所有的边. 分析: 1:以当前节点为根节点,在该节点排士兵守护道路的最小消耗.在这种情况下,他的子节点 ...

随机推荐

  1. rsyslog 同时发生nginx 访问日志和错误日志

    input(type="imfile" File="/var/log/nginx/access.log" Tag="zjzc-frontend01-a ...

  2. 消息队列接口API(posix 接口和 system v接口)

    消息队列 posix API 消息队列(也叫做报文队列)能够克服早期unix通信机制的一些缺点.信号这种通信方式更像\"即时\"的通信方式,它要求接受信号的进程在某个时间范围内对信 ...

  3. C# 导出 excel 复杂格式 html导出

    /// <summary> /// 夜班津贴统计导出 /// </summary> public void ExportOtStat(string data) { var in ...

  4. nm命令中符号类型详解

    摘自http://blog.csdn.net/solmyr_biti/article/details/6565479 nm命令介绍的很多,但大多不介绍其函数符号标志的含义.最近在调试动态库时常用到,其 ...

  5. logback.xml配置详解

    先附上本文分析用的例子: <?xml version="1.0" encoding="UTF-8" ?> <configuration> ...

  6. Avoid The Lakes

    Avoid The Lakes Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  7. js判断的执行顺序

    js预编译是对每一个<script>标签片段进行的.预编译声明所有var变量(初始为undefined),解析定义式函数语句. 还有个关于 "window作用域下,a = 1和v ...

  8. linode最新试用(购买)流程

    最新linode官网www.linode.com可以免费试用7天,但是需要你的身份认证 第一步:注册一个linode账号email填写国内的163.126.qq邮箱是不能认证的,这里可以填写gmail ...

  9. UVA 1613 K-Graph Oddity

    题意; 在一个n个点的无向连通图中,n是奇数,k是使得所有点的度数不超过k的最小奇数,询问一种染色方案,使得相邻点的颜色不同. 分析: k=所有点中最大的入度.如果最大入度是偶数,则k+1.每个点可选 ...

  10. git clone 命令报错 +diffie-hellman-group1-sha1

    解决方法: 在.ssh目录下新建文件config , 添加 Host * KexAlgorithms +diffie-hellman-group1-sha1 到文件config,即可.