poj-2342 Anniversary party(树形DP入门)
来源:互联网 发布:360验机软件 编辑:程序博客网 时间:2024/05/21 10:06
题目链接:http://poj.org/problem?id=2342
Anniversary party
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6060 Accepted: 3488
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
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
711111111 32 36 47 44 53 50 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[N];int flag[N],head[N];int dp[2][N];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;}
0 0
- poj 2342 Anniversary party 树形dp入门
- poj 2342 Anniversary party 树形dp入门
- poj Anniversary party(树形dp入门)
- poj 2342 anniversary party(树形dp入门)
- [ACM] POJ 2342 Anniversary party (树形DP入门题)
- [ACM] POJ 2342 Anniversary party (树形DP入门题)
- POJ 2342 Anniversary party(树形DP入门)
- POJ 2342 Anniversary party (树形dp入门)
- poj-2342 Anniversary party(树形DP入门)
- poj 2342 Anniversary party (树形dp入门)
- poj 2342 Anniversary party (树形dp入门)
- 树形dp入门(poj 2342 Anniversary party)
- poj 2342 Anniversary Party(树形dp)
- POJ 2342 Anniversary party(树形DP)
- poj 2342Anniversary party(树形dp)
- poj 2342 Anniversary party(树形dp)
- POJ 2342Anniversary party(树形DP)
- POJ 2342 Anniversary party (树形dp)
- LeetCode 305. Number of Islands II(小岛)
- Oracle Hint的用法
- Android数据库框架-Archiver(LiteOrm)的使用
- DeviceIOControl详解-各个击破
- 1013&&1014 A strange lift
- poj-2342 Anniversary party(树形DP入门)
- MySQL—SQL优化
- Spring中的设计模式-适配器模式
- iOS测试工具和调试技巧以及内测发布平台总结
- 【Hadoop基础教程】Hadoop之单词计数wordcount
- 【DFS】HDU5547Sudoku
- Rxjava的介绍与使用
- leetcode 123. Best Time to Buy and Sell Stock III
- 仿微信联系人分组(右侧索引)