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 ...
随机推荐
- SpringBoot 整合NoSql
通用配置 maven依赖 添加Spring-Web和Spring-Security依赖,使用Spring-Security是因为使用SpringBoot的Redis依赖时,必须添加Spring-Sec ...
- C语音中最简单的排序冒泡排序和选择排序代码实现(非指针)
#include<stdio.h> int main() { int a[5] = { 2,5,7,3,-1 }; int n = sizeof(a) / sizeof(a[0]);//元 ...
- [LC]876题 Middle of the Linked List (链表的中间结点)(链表)
①中文题目 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5]输出:此列表中的结点 3 (序列化形式 ...
- HTML 颜色输入框修改事件的触发,以及获取修改后的颜色
HTML 颜色输入框修改事件的触发,以及获取修改后的颜色 <!DOCTYPE html> <html lang="en"> <head> < ...
- 【计算机网络】TCP基础知识详解
1. TCP概念相关 [!NOTE] TCP(Transmission Control Protocol),又叫传输控制协议. TCP协议是面向连接的,可靠的,基于字节流的传输协议.在基于 TCP 进 ...
- ZeroC ICE的远程调用框架 Slice如何帮助我们进行Ice异步编程(AMI,AMD)
Slice最大的用处就是为我们使用Ice进行编程,代劳绝大部分的重复性代码,并提供一些帮助性的框架代码,如用于AMI和AMD方式进行异步编程的回调框架. 当Slice不为我们生成代码时,我们仍然可以按 ...
- Centos7編譯安裝LAMP平臺
什麽是LAMP? 拆開看 L 就是Linux系統 A是Apache的縮寫 M.P則是MySQL和PHP的简写. 其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行 ...
- 移动端vue项目的图片上传插件
有一移动端项目,使用的vant-ui.可是vant自带的Uploader似乎不支持一次选择多张图片上传的功能. 于是乎:在https://www.npmjs.com/查找发现找到 vue-upload ...
- 2019-9-24:渗透测试,css样式,js基础学习笔记
css分组和嵌套:分组:比如有<h1><h4><p>,3个标签,设置css时候可以 h1,h4,p{样式:属性} 这样的语法嵌套:比如.lei{样式:属性},.le ...
- day 13 生成器函数 表达式 推导式
今日主要内容 1. 生成器和生成器函数 生成器的本质就是迭代器 生成器的三种创建办法: 1.通过生成器函数 2.通过生成器表达式创建生成器 3.通过数据转换 2. 生成器函数: 函数中包含了yield ...