HDU 1520 Anniversity party
InputEmployees 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 0OutputOutput 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
题解:树形DP。对于每个人,我们储存下比他职位低的下标,并对比他职位低的人打上标记代表这个人有上司。dp[i][1]代表选i这个人,dp[i][0]代表不选这个人,然后从底层开始,对上一层dp[x][1]+=dp[x-1][0];
dp[x][0]+=max(dp[x-1][1],dp[x-1][0]);
然后最大值为: max(dp[root][1],dp[root][0]);root为没有标记的那个人,即没有任何下属的人;
参考代码为:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cstdlib>
#include<set>
using namespace std;
const int maxn=1e4+10;
int N,u,v,w[maxn],vis[maxn],dp[maxn][2];
vector<int> vec[maxn]; void solve(int x)
{
for(int i=0;i<vec[x].size();i++)
{
solve(vec[x][i]);
dp[x][1]+=dp[vec[x][i]][0];
dp[x][0]+=max(dp[vec[x][i]][0],dp[vec[x][i]][1]);
}
} int main()
{
while(~scanf("%d",&N))
{
for(int i=0;i<=N;i++) vec[i].clear();
memset(vis,0,sizeof vis);
memset(dp,0,sizeof dp);
for(int i=1;i<=N;i++) scanf("%d",w+i);
for(int i=1;i<=N;i++) dp[i][1]=w[i];
while(scanf("%d%d",&u,&v), u+v)
{
vec[v].push_back(u);
vis[u]=1;
}
int flag;
for(int i=1;i<=N;i++)
{
if(!vis[i])
{
solve(i);
flag=i;
break;
}
}
int Max=max(dp[flag][1],dp[flag][0]);
printf("%d\n",Max);
}
return 0;
}
HDU 1520 Anniversity party的更多相关文章
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- 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 ...
- HDU 1520 树形DP入门
HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...
- HDU 1520:Anniversary party(树形DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description There i ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1520(简单树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:dp[u][0]表示不取u的最大价值,dp[u][1]表示取u的最大价值,于是有dp[u] ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
随机推荐
- Java设计模式(20):命令模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 智能电脑的品牌越来越多,由此诞生了一款电脑控制的APP,万能遥控器,用户在使用遥控器的时候,可以切换为自家电视的品 ...
- 轻松实现C/C++各种常见进制相互转换
其它进制转为十进制 在实现这个需求之前,先简单介绍一个c标准库中的一个函数: long strtol( const char *str, char **str_end, int base); 参数详细 ...
- 暑假CV-QKD的相关论文单词集(第一弹)
CV-QKD 连续变量-量子秘钥分发 Quadrature 正交 Photon 光子 Coherent 连续的,连贯的 Reconciliation 调解 Cryptograph ...
- 微信小程序(mpvue) wx.openSetting 无法调起设置页面
在开发过程有个需要保存图片/视频到设备相册的业务,so easy~ 巴啦啦撸下来了完整功能, wx.saveVideoToPhotosAlbum 会自动调起用户授权,美滋滋~~ btu.... ...
- [ERROR]element select is not allowed here
问题:在使用IDEA搭建springboot项目的时候,在xml文件中遇到element select is not allowed here错误 原因:xml文件的头部的配置有错误,红框的三个地方命 ...
- gitbook的插件配置
原生的gitbook样式比较单一,美观度和功能欠佳,可通过相关插件进行拓展. 插件地址:https://plugins.gitbook.com/ 主目录下新建book.json: { "au ...
- python:collections模块
Counter类 介绍:A counter tool is provided to support convenient and rapid tallies 构造:class collections. ...
- 用户环境变量 shell变量 别名
常见用户环境变量: 环境变量 说明 LANG HOME LOGNAME 用户名 PATH SHELL PWD 查看环境变量用:env或者echo $LANG 设置用户环境变量:ex ...
- vuejs学习之新的components组件挂载
暂时写个目录,内容待完善,主要是记录我的学习过程,方便以后复习
- 新闻网页通用抽取器GNEv0.04版更新,支持提取正文图片与源代码
GeneralNewsExtractor以下简称GNE是一个新闻网页通用抽取器,能够在不指定任何抽取规则的情况下,把新闻网站的正文提取出来. 我们来看一下它的基本使用方法. 安装 GNE 使用 pip ...