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. 【Xamarin 挖墙脚系列:Windows 10 一个包罗万象的系统平台】

    build2016 结束后,证实了微软之前的各种传言.当然,都是好消息. Windows10 上基本可以运行主流的任意的操作系统. Windows Linux(在内部版本143216中,支持了bash ...

  2. extjs让按钮可用或者不可用

    Ext.getCmp(‘按钮ID’).enable();//设置为可用Ext.getCmp(‘按钮ID’).disable();//设置为不可用

  3. How ASP.NET MVC Works?

    原文地址:http://www.cnblogs.com/artech/archive/2012/04/10/how-mvc-works.html?ADUIN=7783008&ADSESSION ...

  4. Create local metadata resource of yum

    Today, I need install an oracle software for a machine whose os is Linux. As we all know, installing ...

  5. springMVC3学习(四)--訪问静态文件如js,jpg,css

    假设你的DispatcherServlet拦截的是*.do这种URL.就不存在訪问不到静态资源的问题 假设你的DispatcherServlet拦截了"/"全部的请求,那同一时候对 ...

  6. Sequence one(hdu2610dfs+去重)

    题目:有一个数列N,和一个数字k,输出该数列的前k个子序列,如果k大于N的所有子序列,输出所有符合要求的序列,序列要求不能是递减序列 比如: 3 5 1 3 2 的前五个序列为 1 3 2 1 3 1 ...

  7. 小心ThreadLocal的陷阱

    ThreadLocal用在多线程时保存线程级的局部变量,当我们需要在线程内共享数据时,ThreadLocal屡试不爽,但是ThreadLocal也会有一个问题,当你使用线程池时,线程可能会被重用,所以 ...

  8. C#读取csv格式文件

    方法1:用一个System.Web.UI.HtmlControls.HtmlInputFile去handle文件选取 以下是button click event中的code,用来执行当文件选取了之后读 ...

  9. JQuery获取Checkbox组的值

    前台: <div id="addtrtr" style="padding:20px; background-color:#F8F8F8;"> < ...

  10. 使用 Struts 2 实现国际化

    struts2国际化(I18N) 国际化也叫I18N,是Internationalization的简称.Struts2国际化是建立在Java国际化基础上,只是Struts2框架对Java国际化进行了进 ...