ural 1039 树dp
http://acm.timus.ru/problem.aspx?space=1&num=1039
1039. Anniversary Party
Memory limit: 8 MB
Background
president of the Ural State University is going to make an 80'th
Anniversary party. The university has a hierarchical structure of
employees; that is, the supervisor relation forms a tree rooted at the
president. Employees are numbered by integer numbers in a range from 1
to N, The personnel office has ranked each employee with a
conviviality rating. In order to make the party fun for all attendees,
the president does not want both an employee and his or her immediate
supervisor to attend.
Problem
Input
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 the supervisor relation tree goes.
Each line of the tree specification has the form
<L> <K>
0 0
Output
Sample
input | output |
---|---|
7 |
5 |
树上的树dp例题,f[i][0]表示以i为根不包含i可获得的最大价值,f[i][1]表示以i为根包含i在内可获得的最大价值。有f[i][0]+=MAX{f[son[i]][0],f[son[i]][1] } ,f[i][1]+=f[son[i]][0];
因为一旦包含i了显然不能包含i的儿子,所以不能加上f[son[i]][1],反之取二者中较大的就好了。
因为如何保存他们之间的关系想了半天,想用vector来着,看书上写的很简便,利用数组做邻接表处理。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
int c[],fa[],son[],bro[];
int dp[][];
void solve(int id)
{
dp[id][]=;
dp[id][]=c[id];
for(int i=son[id];i!=-;i=bro[i])
{
solve(i);
dp[id][]+=max(dp[i][],dp[i][]);
dp[id][]+=dp[i][];
}
}
int main()
{
int N,i,j,k;
while(cin>>N){
int a,b,root;
memset(fa,-,sizeof(fa));
memset(son,-,sizeof(son));
memset(bro,-,sizeof(bro));
for(i=;i<=N;++i) scanf("%d",c+i);
while(scanf("%d%d",&a,&b)&&(a||b)){
fa[a]=b;
bro[a]=son[b];
son[b]=a;
}
for(i=;i<=N;++i){
dp[i][]=,dp[i][]=c[i];
if(fa[i]==-) root=i;
}
solve(root);
printf("%d\n",max(dp[root][],dp[root][]));
}
return ;
}
ural 1039 树dp的更多相关文章
- 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 ...
- 树形DP URAL 1039 Anniversary Party
题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- HDU4916 Count on the path(树dp??)
这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- HDU4276 The Ghost Blows Light SPFA&&树dp
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- bzoj 3572世界树 虚树+dp
题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...
随机推荐
- app开发团队人员构成怎么分配?国内著名的app开发团队有哪些
app开发团队人员构成:作为一个独立的app开发团队,人员架构必须包括产品经理,程序开发人员,测试专员,运营团队,UI 设计.这里是对专业的App开发公司而言,一般个人或团队可能一个人会身兼多职,所以 ...
- [转载]威力导演14旗舰破解版(中文简体)|取消30天限制CyberLink&nb
2015月9月15日(当地时间),CyberLink讯连科技发布新一代视频编辑软件 — PowerDirector威力导演14,融合了上个版本发布以来的多次更新升级,威力导演依旧 ...
- BADI:LE_SHP_DELIVERY_PROC-增强在交货处理中
1.所得方法清单: CHANGE_FCODE_ATTRIBUTES Control Activation of Function CodesCHANGE_FIELD_ATTRIBUTES Contro ...
- Django基础(三)_分页器、COOKIE与SESSION、FORM表单
分页器(paginator) 分页器的使用 >>> from django.core.paginator import Paginator >>> objects ...
- Django 补充models操作,中间件, 缓存,信号,分页
1.Model 一对多 补充 models如下: class UserType(models.Model): caption = models.CharField(max_length=16) cla ...
- cocos2d关于glew32.lib错误(转)
应项目需要使用cocos2d-x开发,又要学习新东东了.·cocos2d-x 是一个支持多平台的 2D 手机游戏引擎,用C++重写cocos2d-iphone引擎的一个开源项目,想了解更多的童鞋美去百 ...
- linux 基础二---用户群租权限
用户&群组&权限 一.用户 1.用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) / ...
- 测试连接oracle数据库耗时
maven项目 主程序:ConnOracle.java package org.guangsoft.oracle; import java.sql.Connection; import java.sq ...
- HGVS的变异格式
符号: 1.HGVS的变异格式由两部分组成: 1.1 reference sequence file identifier (accession.version-number) : actual d ...
- Hadoop程序基础模板
分布式编程相对复杂,而Hadoop本身蒙上大数据.云计算等各种面纱,让很多初学者望而却步.可事实上,Hadoop是一个很易用的分布式编程框架,经过良好封装屏蔽了很多分布式环境下的复杂问题,因此,对普通 ...