HDU 1520-Anniversary party(树形dp入门)
题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值。
分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1,i参加 能达到的最大欢乐值。i参加i的孩子不能参加,i不参加,其孩子参不惨加都行(取最大值)。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct tree{
int u,v,next;
} t[];
int par[],dp[][],n,head[],len,root;
void add(int a,int b){
t[len].u=a;
t[len].v=b;
t[len].next=head[a];
head[a]=len++;
}
void dfs(int root){
for(int i=head[root];i!=-;i=t[i].next){
int son=t[i].v;
dfs(son);
dp[root][]+=max(dp[son][],dp[son][]);
dp[root][]+=dp[son][];
}
}
int main()
{
while(~scanf("%d",&n)){
memset(par,,sizeof(par));
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
for(int i=;i<=n;++i)
scanf("%d",&dp[i][]);
int c,f;
len=;
while(scanf("%d%d",&c,&f)){
if(c==&&f==)break;
add(f,c);
par[c]=;
}
for(int i=;i<=n;++i){
if(!par[i]){
root=i;
break;
}
}
dfs(root);
printf("%d\n",max(dp[root][],dp[root][]));
}
return ;
}
HDU 1520-Anniversary party(树形dp入门)的更多相关文章
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu oj 1520 Anniversary party(树形dp入门)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- [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 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...
- hdu 1011 Starship Troopers(树形DP入门)
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520
题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...
- [HDU 1520] Anniversary party
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 树形dp 入门
今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...
随机推荐
- 区间型动规--石子归并(Pascal)
题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使 ...
- 第二好用的时间日期选择插件(jscal)
这个是第二好用的了,支持鼠标滚动选择时间.功能很强大,文档:http://www.dynarch.com/jscal/ 效果图: <!DOCTYPE html PUBLIC ...
- python字符串内容替换的方法(转载)
python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法. ...
- HTML5的本地存储 LocalStorage
localStorage顾名思义,就是本地存储的意思,在以前很长一段时间,要想在客户端存 储一些配置及登录信息等数据都只能通过COOKIE或flash的方式,如今html5来临,也 带来了更强大的本地 ...
- linux fork函数与vfork函数
一.fork1. 调用方法#include <sys/types.h>#include <unistd.h> pid_t fork(void);正确返回:在父进程中返回子进程的 ...
- overrides final method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet 错误解决
使用java代码连接hbase服务器报错: java.lang.VerifyError: class org.apache.hadoop.hdfs.protocol.proto.ClientName ...
- MVC 文件上传和图片剪辑
http://www.cnblogs.com/hinton/archive/2012/03/01/2375465.html http://gallery.kissyui.com/uploader/1. ...
- FileZilla Server 防火墙端口开启设置 windows 2008 win
入站规则 添加21端口, 程序FileZilla server.exe 出站规则 %SystemRoot%\System32\ftp.exe
- 二维图形的矩阵变换(三)——在WPF中的应用矩阵变换
原文:二维图形的矩阵变换(三)--在WPF中的应用矩阵变换 UIElement和RenderTransform 首先,我们来看看什么样的对象可以进行变换.在WPF中,用于呈现给用户的对象的基类为Vis ...
- Servlet入门案例
开发servlet有三种方法: 1.实现Servlet接口; public interface Servlet { void init(ServletConfig var1) throws Servl ...