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 ...
随机推荐
- JavaScript的语法要点 1 - Lexically Scoped Language
作为从一开始接触C.C++.C#的程序员而言,JavaScript的语法对我来说有些古怪,通过最近一年的接触,对它有了一定的了解,于是想把它的一些语法要点记录下来. 1. Block Scope vs ...
- 常用PHP运行环境一键安装包
PHP的程序很火,像博客:Wordpress.论坛:Discuz!.SNS:UCenter Home.CMS:DedeCMS等等都是比较流行的PHP程序,大家都在使用PHP类的程序,但对于很多初学者来 ...
- [转]内嵌WORD/OFFICE的WINFORM程序——DSOFRAMER使用小结
最近一直想用VC#2005做个内嵌WORD/OFFICE的WINFORM程序,目前主要有以下解决途径: 1.直接通过API把WORD/OFFICE的窗口句柄给放到WINFORM中(感觉较为复杂): 2 ...
- python模块之os和os.path模块
1.os模块os.listdir(dirname) 列出dirname下的目录和文件os.getcwd()函数得到当前工作目录,即当前Python脚本工作的目录路径.os.getenv()和os.pu ...
- spring IOC源码分析(3)
1.IOC容器的依赖注入 Spring中,依赖注入是在用户第一次向IOC容器索要Bean时触发的(通过getBean方法). 在BeanFactory中我们看到getBean(String…)函数,它 ...
- JQ实现3D拖拽效果
<!DOCTYPE HTML> <html onselectstart='return false'> <head> <meta http-equiv=&qu ...
- javascript移动设备触屏事件
ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件: /** * onTouchEvent */ var div = d ...
- 解决Linux/aix 下的websphere log4j不生效
websphere 解决Linux/aix下的log4j不生效 在目录: /IBM/WebSphere/AppServer/profiles/AppSrv01/properties 增加一个文件:可以 ...
- 黑客是怎样绕过WAF之三重防护绕过讲解
什么是WAF Web Application Firewall 通过执行一系列针对HTTP/HTTPS的安全策略来防御对Web应用的攻击. 目前主要有单设备WAF与云WAF WAF的现状 1.太多数W ...
- 如何使用 Java8 实现观察者模式?(上)
[编者按]本文作者是 BAE 系统公司的软件工程师 Justin Albano.在本篇文章中,作者通过在 Java8 环境下实现观察者模式的实例,进一步介绍了什么是观察者模式.专业化及其命名规则,供大 ...