POJ 2342 (树形DP)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3863 | Accepted: 2172 |
Description
the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your
task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.
Input
After that go N – 1 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 0
Output
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,在整个树上跑一遍dfs,设dp[s][0]表示不取s时以s为跟节点的子树的最大搞笑值,dp[s][1]表示取s时以s为跟节点的子树的最大搞笑值,则状态转移方程为:
dp[s][0] += max(dp[u][0], dp[u][1]);
dp[s][1] += dp[u][0];
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 6010
using namespace std;
int dp[MAXN][2], rat[MAXN];
int vis[MAXN], head[MAXN];
typedef struct{
int to, next;
}Edge;
Edge edge[MAXN];
void addedge(int u, int v, int k){
edge[k].to = v;
edge[k].next = head[u];
head[u] = k;
}
void dfs(int s){
dp[s][0] = 0;
dp[s][1] = rat[s];
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
dfs(u);
dp[s][0] += max(dp[u][0], dp[u][1]);
dp[s][1] += dp[u][0];
}
}
int main(){
int n, u, v, father;
freopen("in.c", "r", stdin);
while(~scanf("%d", &n)){
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
for(int i = 1;i <= n;i ++) scanf("%d", rat+i);
for(int i = 1;i <= n-1;i ++){
scanf("%d%d", &u, &v);
addedge(v, u, i);
vis[u] = 1;
}
scanf("%d%d", &u, &v);
for(int i = 1;i <= n;i ++){
if(!vis[i]){
father = i;
break;
}
}
dfs(father);
printf("%d\n", max(dp[father][0], dp[father][1]));
}
return 0;
}
POJ 2342 (树形DP)的更多相关文章
- POJ 2342 树形DP入门题
有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...
- POJ 2342 (树形DP)
题目链接: http://poj.org/problem?id=2342 题目大意:直属上司和下属出席聚会.下属的上司出现了,下属就不能参加,反之下属参加.注意上司只是指直属的上司.每个人出席的人都有 ...
- poj 2342树形dp板子题1
http://poj.org/problem?id=2342 #include<iostream> #include<cstdio> #include<cstring&g ...
- Anniversary party(POJ 2342 树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5767 Accepted: 3335 ...
- Fire (poj 2152 树形dp)
Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...
- poj 1463(树形dp)
题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...
- poj 2486( 树形dp)
题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...
- poj 3140(树形dp)
题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...
- Strategic game(POJ 1463 树形DP)
Strategic game Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 7490 Accepted: 3483 De ...
- poj 3345 树形DP 附属关系+输入输出(好题)
题目连接:http://acm.hust.edu.cn/vjudge/problem/17665 参考资料:http://blog.csdn.net/woshi250hua/article/detai ...
随机推荐
- Linux负载均衡概念与实践(一)
根据网上文章整理. 负载均衡软件LVS(Linux Virtual Server)概念篇 lvs是在linux操作系统基础上建立虚拟服务器,实现服务节点之间的负载均衡.它是基于linux内核实现的.2 ...
- display:block; 块级元素。<a>,<span>标签设置宽度和高度
display:block;是让对象成为块级元素(比如a,span等) 转化后 可以对a或者span标签进行width和height设置,否则设置不了 display有很多对象,具体可以参考http: ...
- 我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!
第一部分:Html5市场的力量 我们太忽略Html5的市场力量了.如果你把Html5当作一种技术,就大错特错了!忘掉你的产品,忘掉你的技术,想想移动时代的信息传播和消费场景.作为2B,我们实在是没有重 ...
- 电脑升级完Xcode8后 注释快捷键无效的问题
1.部分电脑升级完Xcode8 后直接重启电脑就可以使用Command +/ 快捷键注释代码, 2.如果上述方法没有效果,可以在终端输入sudo /usr/libexec/xpccachectl 然 ...
- SQL技术内幕二DDL
创建数据库: if db_id('DBTest') is nullcreate database DBTest 创建表 use eb_fy_data_test---use 切换所在数据库上下文 if ...
- R语言语法笔记
## 1. 数据输入 ## a$b # 数据框中的变量 a = 15 # 赋值 a <- 15 # 赋值 a = c(1,2,3,4,5) # 数组(向量) b = a[1] # 数组下标,从1 ...
- 转:testlink 环境搭建(傻瓜版)
testlink 环境搭建(傻瓜版) 2011-11-24 22:23 by 虫师, 12322 阅读, 4 评论, 收藏, 编辑 今天抽了点时间把testlink 环境搭建了一下,一直觉得这东西不怎 ...
- Python设计模式——模版方法模式
1.模版方法模式 做题的列子: 需求:有两个学生,要回答问题,写出自己的答案 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Stude ...
- hdu 4300 Clairewd’s message KMP应用
Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...
- 今年的IT大趋势是虚拟现实
从今年下半年开始,陆陆续续出现了一些基于虚拟现实技术的创业公司,先是从IT新闻中的一篇创业故事中了解到这个方向,后来再是身边一个以前的朋友也发布了类似的产品. 从他们的产品来看,基本都是围绕教育行业开 ...