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 ...
随机推荐
- static 关键字用法
static a=0; 就是把a初始化为0:初始值为0而已 即使a是局部变量,每次进入此变量所在的函数,a值还是保持上次赋值: 在中断里建议在局部变量前加上static,以确保此变量值的寿命
- js当前日期
function CurentTime() { var now = new Date(); var year = now.getFullYear(); ...
- jquery获取当前被选择的复选框的value的集合
1.HTML代码 <input type="checkbox" name="productID" value="0"> < ...
- webservice、soap、wsdl
搜集了一些关于webservice.soap.wsdl的基本知识,解决工作中遇到的疑问 一 .什么是webservice(用你的话描述webservice)?在什么时候用webservice(webs ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest H Milestones1 (暴力)
预处理+暴力,每个颜色都是独立的,求个前缀和,减一减判断一个在区间内颜色是否存在. 算了算复杂度好像有点勉强,但是还是过了,学了主席树以后用主席树在做一下 #include<bits/stdc+ ...
- cv2.Laplacian 模糊判断
简介 cv2.Laplacian是用来判断图像模糊度的 函数原型 dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta ...
- Python-OpenCV——亮度和对比度
亮度与对比度 亮度调整是将图像像素的强度整体变大/变小,对比度调整指的是图像暗处变得更暗,亮出变得更亮,从而拓宽某个区域内的显示精度. OpenCV中亮度和对比度应用这个公式来计算:g(x) = αf ...
- Beta冲刺(周四)
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...
- stixel-world跑在kitti数据集
kitti数据集中每一帧的Calibration不同,每一帧都存储了4个相机的Calibration http://ww.cvlibs.net/publications/Geiger2013IJRR. ...
- python_88_xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单例如创建xmltest.xml文件内容如上 注:/代表自结束符号 <?xml version=&quo ...