POJ:2342-Anniversary party(树形dp入门题目)
传送门:http://poj.org/problem?id=2342
Anniversary party
Time Limit: 1000MS Memory Limit: 65536K
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 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
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
- 题意很简单就是公司要举办一个party,但是每个人都不想和自己的直属上司一同在party里面,每个人如果参见party有一个欢乐值,要求你计划安排要举办的party欢乐值最大。
- 其实就是一个树形dp,dp[i][j]代表第i个人是否来(j为1代表来,j为0代表不来),然后就是状态转移方程:dp[root][0] = max(dp[pre_root][1],dp[pre_root][0];
dp[root][1] += dp[pre_root][0]; - 主要考察的就是一个建树的过程,以及寻找根节点,关于dp的部分还是很简单的。
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 6010;
int dp[maxn][3],n;
bool vis[maxn];//用来查找根节点
vector<int> ve[maxn];//用来存树
void init()
{
for(int i=0;i<=n+1;i++)
ve[i].clear();
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
scanf("%d",&dp[i][1]);
int S,E;
while(scanf("%d%d",&S,&E) && S+E)
{
ve[E].push_back(S);
vis[S] = true;
}
}
void dfs(int x)
{
for(int i=0;i<ve[x].size();i++)
{
int temp = ve[x][i];
dfs(temp);
//状态转移方程
dp[x][0] += max(dp[temp][0],dp[temp][1]);
dp[x][1] += dp[temp][0];
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=1;i<=n;i++)
if(!vis[i])
{
dfs(i);
printf("%d\n",max(dp[i][0],dp[i][1]));
break;
}
}
return 0;
}
POJ:2342-Anniversary party(树形dp入门题目)的更多相关文章
- 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 Description There is going to be a party to celebrate the 80-th ...
- POJ 2342 Anniversary party 树形DP基础题
题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- [poj2342]Anniversary party树形dp入门
题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...
- POJ 2342 Anniversary party (树dp)
题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...
- 树形DP入门题目推荐以及解析
关于树形DP几道入门题目 今天恶补树形DP,感觉海星. 其实挺简单的. 介绍几道例题,我会的. 1.洛谷P1352 没有上司的舞会 我的一篇题解 我们可以考虑每一个节点都是有两种情况. 一个是被邀请: ...
- POJ 1463 Strategic game(树形DP入门)
题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...
- poj 2342 && hdu 1520 树形dp
题意:有n个人,接下来n行是n个人的价值,再接下来n行给出l,k说的是l的上司是k,这里注意l与k是不能同时出现的 链接:点我 dp[i][1] += dp[j][0], dp[i][0] += ma ...
随机推荐
- SpringBoot---Web开发---Tomcat配置
- 项目部署到centos7云端验证码出现乱码
原因:linux系统字体和设置验证码的字体不一致 参考文献: https://www.whatled.com/post-6169.html 本次项目我使用的字体是 Arial
- MVC View与Controller分离
新建了一个 Separate 解决方案, 如下图 Separate.UI UI层. 引用 Separate.Home Separate.Home 把Home控制器分享到 一个类库中 并引用(Sy ...
- MyBatis配置文件之properties属性
MyBatis提供3个方式使用properties: 1.property子元素. 2.properties文件. 3.程序代码传递. properties属性系给系统配置一些运行参数,一般放在XML ...
- jeecg308自定义使用getDataGridReturn方法分页失效问题
DataGrid dataGrid = new DataGrid(); dataGrid.setPage(p); dataGrid.setRows(r); dataGrid.setOrder(&quo ...
- Spring 整合 Quartz 实现动态定时任务(附demo)
最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...
- ABAP:parameters的用法
parameters 1.基础用法 parameters:p0(20) type c. 2.使用DEFAULT后缀为参数指定缺省值. parameters:p1(20) type c default ...
- Web前端体系的脉络结构
Web前端技术由 html.css 和 javascript 三大部分构成,是一个庞大而复杂的技术体系,其复杂程度不低于任何一门后端语言.而我们在学习它的时候往往是先从某一个点切入,然后不断地接触和学 ...
- 代码管理_Git中获取两个标签之间的变更代码行数
操作步骤: 1.先进入git的服务器,定位到 repositories 目录 2.再定位到具体的项目目录(登录bitbucket查看项目属性可以看到项目的存放目录),如下图: 3.浏览项目的标签,命令 ...
- iPad开发简单介绍
iPad开发最大的不同在于iPhone的就是屏幕控件的适配,以及横竖屏的旋转. Storyboard中得SizeClass的横竖屏配置,也不支持iPad开发. 1.在控制器中得到设备的旋转方向 在 i ...