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 ...
随机推荐
- HTTP的请求方式
GET 请求获取 Request-URI 所标识的资源POST 在 Request-URI 所标识的资源后附加新的数据HEAD 请求获取由 Request-URI 所标识的资源的响应消息报头PUT ...
- pat 1008 Elevator(20 分)
1008 Elevator(20 分) The highest building in our city has only one elevator. A request list is made u ...
- goland_beego框架学习--api实现
goland_beego框架学习--api实现 完成一项api实现的流程 (1)beego框架的router层里面注册路由 正则路由 为了用户更加方便的路由设置,beego 参考了 sinatra 的 ...
- 【2018寒假集训 Day2】【动态规划】挖地雷
挖地雷(Mine) 在一个地图上有N 个地窖(N<=200),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径,并规定路径都是单向的,且从编号小的地窖通向编号大的地窖.某人可以从任一处 ...
- 网页解析之BeautifulSoup
介绍及安装 Beautiful Soup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. BeautifulSoup 用来解析 HTML 比较简单,API非常人 ...
- Fragment源码分析
转载请标明出处:http://blog.csdn.net/shensky711/article/details/53171248 本文出自: [HansChen的博客] 概述 Fragment表示 A ...
- MySQL双主+keepalived实现高可用实现(热备)
环境:centos6.7 最小化安装 192.168.100.152 master 主192.168.100.153 slave 从192.168.100.132 v_ip 浮动IP 配置ssh密码登 ...
- Chapter 02—Creating a dataset(Part1)
一. 数据集 1. 在R语言中,进行数据分析的第一步是创建一个包含待研究数据并且符合要求的数据集. · 选择装数据的数据结构 · 把数据装入数据结构中 2. 理解数据集 (1)数据集通常是矩形的数据列 ...
- Chapter 01—Introduction to R
1.getwd():list the current working directory. (即获得当前工作路径) 2.setwd("mydirectory"):change th ...
- Bootstrap 元素居中设置
一.Bootstrap水平居中 1. 文本:class ="text-center" 2. 图片居中:class = "center-block" 3.其他元素 ...