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. 一次使用Eclipse Memory Analyzer分析Tomcat内存溢出(转)

    前言 在平时开发.测试过程中.甚至是生产环境中,有时会遇到OutOfMemoryError,Java堆溢出了,这表明程序有严重的问题.我们需要找造成OutOfMemoryError原因.一般有两种情况 ...

  2. pyspark 写 logistic regression

    import random as rd import math class LogisticRegressionPySpark: def __init__(self,MaxItr=100,eps=0. ...

  3. #include <boost/thread.hpp>

    在这个库最重要的一个类就是boost::thread,它是在boost/thread.hpp里定义的,用来创建一个新线程.它已经被纳入C++标准库中. 小结:新一代C++标准将线程库引入后,将简化多线 ...

  4. Windows下安装Python3.4.2

    一.Windows下安装Python3.4.2 1.下载Windows下的Python3.4.2.exe 2.指定一个目录安装,然后下一步 3.配置环境变量包括Python.exe的文件.目录如下图所 ...

  5. 命令行运行命令时报错You don&#39;t have write permissions for the /Library/***

    这是由于要运行这些操作时必须有管理员的权限(比方更新软件),比方更新cocoapods时报错 soindy:SmartThermo soindy$ gem install cocoapods Fetc ...

  6. 编译hadoop2.2.0源码时报错

    编译hadoop2.2.0源码时, mvn install -DskipTests 报错: [ERROR] COMPILATION ERROR : [INFO] ------------------- ...

  7. 济南最新公交线路一览(BRT)

    济南最新公交线路一览(BRT) 济南BRT1路 高速公交线路 黄岗路东5:30-21:30 全福立交桥西5:30-21:30 票价1元,刷卡9折,月票有效 高速公交公司 去程:黄岗路东(BRT) - ...

  8. Ror初学笔记

    Ror正在以惊人的速度增长着,特别是在常常光顾JavaEye的时候发现Ror已经在国内有非常好的基础了,当然要凑个热闹尝尝鲜 咯. 眼下国内Ror的中文资料还是非常少的,到网上找找就仅仅有Eiffel ...

  9. 【转】Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask

    方法一:(java习惯,在android不推荐使用) 刚刚开始接触android线程编程的时候,习惯好像java一样,试图用下面的代码解决问题 new Thread( new Runnable() { ...

  10. S5700交换机配置端口镜像

    S5700交换机配置端口镜像 <Quidway>system-view    //进入系统视图 Enter system view, return user view with Ctrl+ ...