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 ...
随机推荐
- C语言中的程序终止函数
在C语言的标准库<stdlib.h>中提供了一些与正常或者不正常的程序终止有关的函数,下面分别对其进行简单介绍. 参考文献: [1] C和指针,P298,342 [2] C程序设计语言现代 ...
- iis7.5 aspx,ashx的mime类型
映射aspx: 打开IIS管理器,找到“处理程序映射”,在列表右击选择“添加脚本映射”即可.eg:*.aspx,将该类型的页面的处理程序映射为“%windir%\Microsoft.NET\Frame ...
- SQL Server2008数据库导入导出兼容性处理
使用场景:SQL Server 的高版本数据库恢复到低版本则可能会有兼容性问题,为了解决此类问题进行数据库脚本模式处理,数据库结构,及数据存储更换版本等. 1. 选择要导出的数据库,右键任务,生成脚 ...
- web api写api接口时返回
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- li样式不显示使用overflow:hidden导致Li前面点、圈等样式不见
点评:用了overflow:hidden 会影响 list-style,即当ul 中的li 的overflow 为hidden的时候,list-style不起作用,不显示前面的点.圈等样式,在ul或l ...
- 1-了解Python
为什么使用python: 软件质量: 可读写.一致性.软件质量 支持软件开发的高级重用机制 提供开发者的效率: 代码只有java或C++的1/5~1/3 无须编译链接,提高了程序原的效率 程序的可移植 ...
- web2py--------------用web2py写 django的例子 --------开发环境
我们先从广为人知的例子说起xi 也就是官方的例子,我会在最后给出代码: ============================环境=================== 编译器使用vs code , ...
- splay学习笔记
伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.(来自百科) 伸展树的操作主要是 –rotate(x) 将x旋转到x的父亲的位置 voi ...
- httpcontext in asp.net unit test
[TestMethod] [HostType("ASP.NET")] [UrlToTest("http://localhost:25153/qq/a.aspx" ...
- iOS+JSPatch在线修改app功能-b
什么是热更新? 举个例子,你的app上架了,但是突然想添加个小功能,那么你有两种方法 第一种方法:在原生代码中修改源代码,然后提交到appStore,这个过程真是很漫长...虽然最近我提交的都是一两天 ...