poj2342 Anniversary party【树形dp】
来源:互联网 发布:剑三苍云成女捏脸数据 编辑:程序博客网 时间:2024/04/29 16:25
Anniversary party
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4567 Accepted: 2594
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
#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;const int MAX=6005;int father[MAX],vis[MAX],value[MAX],dp[MAX][2],n;int max(int a,int b){return a>b?a:b;}void DFS(int x){vis[x]=1;for(int i=1;i<=n;++i){if(father[i]==x){if(vis[i])continue;DFS(i);dp[x][1]+=dp[i][0];dp[x][0]+=max(dp[i][0],dp[i][1]);}}}int main(){int i,j,a,b;scanf("%d",&n);for(i=1;i<=n;++i)scanf("%d",&dp[i][1]);memset(father,-1,sizeof(-1));while(1){scanf("%d%d",&a,&b);if(a==0&&b==0)break;father[a]=b;}int root=1;while(father[root]!=-1)root=father[root];DFS(root);printf("%d\n",max(dp[root][0],dp[root][1]));return 0;}
0 0
- POJ2342:Anniversary party(树形DP)
- poj2342 Anniversary party【树形dp】
- POJ2342 Anniversary party(树形DP)
- poj2342.Anniversary party(树形dp)
- POJ2342 Anniversary party 【树形dp】
- poj2342 Anniversary party (树形DP)
- POJ2342 Anniversary party(树形dp)
- poj2342|hdu 1520 Anniversary party 树形dp
- hdu 1520 && poj2342 anniversary party树形DP
- Hdoj 1520&Poj2342 Anniversary party 【树形DP】
- POJ2342 Anniversary party 树形dp入门题
- poj2342 Anniversary party(树形dp)
- hdu1520 Anniversary party(poj2342,树形dp)
- URAL 1039 / poj2342-Anniversary Party-树形DP
- [树形dp][入门]hdu1520 & poj2342 Anniversary party
- POJ2342 ->Anniversary party(树形DP入门题)
- poj2342 Anniversary party--树形dp入门
- POJ2342:Anniversary party(树形dp)
- Python字符串格式化
- Hibernate连接多个数据库
- Sicily 1006 Team Rankings
- LeetCode 002 AddTwoNumbers
- C++ 纯虚函数与抽象类
- poj2342 Anniversary party【树形dp】
- 机房重构前奏——三层转七层
- android编译系统makefile(Android.mk)写法
- tomcat7配置管理员帐号密码
- Windows API 获取屏幕分辨率 GetSystemMetrics ( )
- Corusera-Foundmental English Writing-Subjects 主语
- 英语总结系列(二):爽约Pauel叔十五天
- 杭电1002----高精度计算(加法)
- 随机抽样——蓄水池抽样算法(Reservoir Sampling)