poj 2342(树型dp)
来源:互联网 发布:怎么在淘宝上开充值店 编辑:程序博客网 时间:2024/05/19 14:15
Anniversary party
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7169 Accepted: 4135
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
题意:
每个人有一个宴会值,有一个树型的职位关系图,每个人不能和自己的直系上司参加宴会,最后求出办一场宴会的最大宴会值。
思路:
把职位关系建成一棵树,记录每个人的直系上司。
每个人的dp数组为dp[n][2]。dp[i][0]代表这个人不出席,dp[i][1]代表这个人出席。
递推公式为:
dp[i][0]=max(dp[j][1],dp[j][0])
dp[i][1]=dp[j][0]
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#include<bitset>#include<queue>#include<stack>#include<list>#include<set>#include<math.h>#include<map>using namespace std;int n,m,dp[6005][2],fa[6005],vis[6005];void treedp(int node){ vis[node]=1; for(int i=1;i<=n;i++) { if(!vis[i]&&fa[i]==node) { treedp(i); dp[node][0]+=max(dp[i][1],dp[i][0]); dp[node][1]+=dp[i][0]; } }}int main(){freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);scanf("%d",&n);memset(dp,0,sizeof(dp));memset(fa,0,sizeof(fa));memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++) { scanf("%d",&dp[i][1]); } int u,v; while(scanf("%d%d",&u,&v)) { if(u==0&&v==0) break; fa[u]=v; } int root=1; while(fa[root]) root=fa[root]; treedp(root); printf("%d\n",max(dp[root][1],dp[root][0]));return 0;}
0 0
- poj 2342(树型dp)
- poj 2342 树型dp入门
- 树形dp poj 2342
- poj 2342 树形DP
- poj 2342 树形DP
- poj 2342 树状DP
- poj 2342 树形DP
- poj 2342 树形dp
- POJ 2342 树形dp
- poj 3107 树型DP
- poj 2378 树型DP
- poj 1655 树型DP
- poj 3140 树型DP
- poj 2378 树型dp
- poj 1155(树型dp)
- POJ 2342 简单树形dp
- POJ 2342 (树形DP)
- poj 2342 树形dp入门
- Java时间格式化
- JXLS 利用excel模板生成excel文件
- 详解重载解析(一)
- jQuery EasyUI 表单 - 创建树形下拉框
- 美国指责俄罗斯是电邮黑客幕后推手 俄方回应
- poj 2342(树型dp)
- 线程安全性——从StringBuffer和StringBuilder的区别说起
- [LeetCode]Rotate Image
- iOS UINavigationController导航条背景透明
- Python_正则表达式
- 深度学习中的数学与技巧(3):从Bayesian角度浅析Batch Normalization
- 递归n次幂
- 页面加载时遮罩效果
- support-v4拆分