poj-2342 Anniversary party(树形DP入门)
2016-04-19 13:15
465 查看
题目链接:http://poj.org/problem?id=2342
Anniversary party
Description
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V.
E. Tretyakov. In order to make 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
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. 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 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
Output should contain the maximal sum of guests' ratings.
Sample Input
Sample Output
题意:有n个员工,每个员工都有一个权值(可为负)。然后有n-1条语句,每条两个数字i,j,表示j是i的上司。 从这几个人中选出几个人,要求相互之间不能有直接上下级关系,求最大的权值。注意可以一个人都不选,结果为0.
思路:一看就是树形DP,因为员工之间是一个树的关系。
代码:
Anniversary party
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6060 | Accepted: 3488 |
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V.
E. Tretyakov. In order to make 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
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. 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 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
Output should contain the maximal sum of guests' ratings.
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
题意:有n个员工,每个员工都有一个权值(可为负)。然后有n-1条语句,每条两个数字i,j,表示j是i的上司。 从这几个人中选出几个人,要求相互之间不能有直接上下级关系,求最大的权值。注意可以一个人都不选,结果为0.
思路:一看就是树形DP,因为员工之间是一个树的关系。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 6050 #define minn -(1<<25) struct Tree { int val,next; } edge ; int flag ,head ; int dp[2] ; int cnt; void addedge(int s,int e) { edge[cnt].val=e; edge[cnt].next=head[s]; head[s]=cnt++; } void dfs(int node) { for(int i=head[node]; i!=-1; i=edge[i].next) { int val=edge[i].val; dfs(val); dp[1][node]=max(dp[1][node],dp[1][node]+dp[0][val]); dp[0][node]+=max(dp[1][val],dp[0][val]); } } int main() { int n,s,e,m; while(~scanf("%d %d",&n,&dp[1][1])&&(n+dp[1][1])) { cnt=0; for(int i=2; i<=n; i++) scanf("%d",&dp[1][i]); memset(head,-1,sizeof(head)); memset(flag,0,sizeof(flag)); memset(dp[0],0,sizeof(dp[0])); for(int i=1; i<n; i++) { scanf("%d %d",&e,&s); flag[e]=1; addedge(s,e); } for(int i=1; i<=n; i++) if(!flag[i]) { m=i; break; } memset(flag,0,sizeof(flag)); dfs(m); int maxn=minn; if(maxn<dp[1][m]) maxn=dp[1][m]; if(maxn<dp[0][m]) maxn=dp[0][m]; printf("%d\n",maxn); } return 0; }
相关文章推荐
- Spring中的设计模式-适配器模式
- iOS测试工具和调试技巧以及内测发布平台总结
- 两段代码告诉你xml中 @+id 与 @id 的区别
- MySQL—SQL优化
- Java实现:数据结构之排序
- web开发-服务器Controller到前端中的数据传递
- c3p0、dbcp、tomcat jdbc pool 连接池区别(推荐使用jdbc pool)
- Spearman's rank correlation coefficient 和 Pearson correlation coefficient详细
- PopupWindow的使用方法
- ThinkPad T440p如何默认F1~F12键
- Android数据库框架-Archiver(LiteOrm)的使用
- 1013&&1014 A strange lift
- DeviceIOControl详解-各个击破
- Oracle Hint的用法
- 让PS可以保存为ICO格式
- 仙剑三-诉衷情.旖旎情
- LeetCode 305. Number of Islands II(小岛)
- 在Ubuntu的系统中怎样将应用程序加入到開始菜单中
- Pydev下载地址
- maven安装本地jar到仓库