[ACM] POJ 2342 Anniversary party (树DP获得冠军)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4410 | Accepted: 2496 |
Description
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
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
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
解题思路:
题意为有n个人要开一个PARTY。编号1到n。每一个人都有一个欢乐值。而且每一个人都有一个直接上司,为了让气氛更好,要求在这n个人中选一些人去參加PARTY,而且选出的这些人中随意两个人之间都没有直接上司或直接下属关系。求选出人的最大欢乐值。
这n个人通过直接上司或直接下属关系构成了一棵树,假设把父节点看做直接上司。那么子节点为下属,并且上司和下属不能同一时候选择。
定义dp[i][0] 为第i个人不选择所获得的最大欢乐值,dp[i][1] 为第i个人被选择所获得的最大欢乐值
如果 j 是第i个人的下属。 那么有转移方程 :
dp[i][0]+=max( dp[j][0],dp[j][1]); 注意是+=,由于一个父节点有多个子节点
dp[i][1]+=dp[j][0];
用DFS遍历这棵树,每一个顶点被訪问,且仅仅被訪问一次。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=6005;
vector<int>son[maxn];
bool vis[maxn];
int dp[maxn][2];
int n; void dfs(int root)
{
vis[root]=1;
for(int i=0;i<son[root].size();i++)
{
int v=son[root][i];
if(!vis[v])
{
dfs(v);
dp[root][1]+=dp[v][0];
dp[root][0]+=max(dp[v][0],dp[v][1]);
}
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<=n;i++)
son[i].clear();
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
scanf("%d",&dp[i][1]);
dp[i][0]=0;
}
int fa,so;
while(scanf("%d%d",&so,&fa)!=EOF)
{
if(so==0&&fa==0)
break;
son[fa].push_back(so);
son[so].push_back(fa);
}
dfs(1);
cout<<max(dp[1][0],dp[1][1])<<endl;
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[ACM] POJ 2342 Anniversary party (树DP获得冠军)的更多相关文章
- POJ 2342 Anniversary party (树dp)
题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- poj 2342 Anniversary party 树形DP入门
题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...
- POJ 2342 Anniversary party 树形DP基础题
题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...
- 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 ...
- DP Intro - poj 2342 Anniversary party
今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- poj 2342 Anniversary party_经典树状dp
题意:Ural大学有n个职员,1~N编号,他们有从属关系,就是说他们关系就像一棵树,父节点就是子节点的直接上司,每个职员有一个快乐指数,现在要开会,职员和职员的直接上司不能同时开会,问怎才能使开会的快 ...
- POJ 2342 Anniversary party(树形dp)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7230 Accepted: 4162 ...
随机推荐
- 【Android Training - UserInfo】记住登入用户的信息[Lesson 1 - 使用AccountManager来记住用户]
Remembering Your User[记住你的用户] 每一个人都非常喜欢自己的名字能被人记住.当中最简单,最有效的使得你的app让人喜欢的方法是记住你的用户是谁,特别是当用户升级到一台新的设备或 ...
- Windows Phone开发(39):漫谈关键帧动画上篇
原文:Windows Phone开发(39):漫谈关键帧动画上篇 尽管前面介绍的几种动画会让觉得很好玩了,但是,不知道你是否发现,在前面说到的一系列XXXAnimation中,都有一个共同点,那就是仅 ...
- InputStreamReader 和 OutputStreamWriter类使用方法简单介绍,及演示。
InputStreamReader 和 OutputStreamWriter类使用方法简单介绍. 一.InputStreamReader类 InputStreamReader 将字节流转换为字符流.是 ...
- [改变自己wordpress.2]至wordpress再加上简单的debug sql调试.
或者说,同事. 需要帮助她打印出来sql 调试输出到页面sql 在这里,我们使用插件或一个的方式来启动配置文件wordpress的debug 在插件文件夹 wordpress/wp-content/p ...
- 玩转web之json(五)---将表单通过serialize()方法获取的值转成json
form表单有一个serialize()方法,可以序列化表单的值,但是jquery提供的这个方法会把数据序列化为类似下面的形式: a=1&b=2&c=3&d=4 jquery并 ...
- ADO.NET连接方式
使用Command.DataReader和DataSet两种方法实现数据绑定 方法1:使用Command和DataReader SqlConnection con = new SqlConnectio ...
- Windows Phone 8 - Runtime Location API - 2
原文:Windows Phone 8 - Runtime Location API - 2 在<Windows Phone 8 - Runtime Location API - 1>介绍基 ...
- JAVA基础编程50题(10-12题)具体解释
一.描写叙述 1.一球从m米高度自由落下.每次落地后反跳回原高度的一半:再落下,求它在 第n次落地时.共经过多少米?第10次反弹多高? 2.有1.2.3.4个数字.能组成多少个互不同样且无反复数字的三 ...
- DirectShow基础编程 最简单transform filter 编写步骤
目标编写一个transform filter,功能是对图像进行翻转. 一.选择基类 从CBaseFilter派生出三个用于编写transform filter的类,各自是:CTransformFilt ...
- Libgdx Box2D现实------这缓释微丸(一个:项目介绍)
它花了两个星期的假写物理游戏,在几次课逃逸,是大学,因为大部分时间点!今天,我基本上一直每节课的点1-3有时它,哎,这似乎是不再逃跑.不知道值没有值得,仅仅是简单地想做自己喜欢的事情,而不是跟老师大眼 ...