POJ Anniversary party By Assassin
来源:互联网 发布:软件项目维护合同范本 编辑:程序博客网 时间:2024/05/14 16:12
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
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
第一步:确定状态
用f[i][0]表示不选择i点时,i点及其子树能选出的最多人数,f[i][1]表示选择i点时,i点及其子树的最多人数。
第二步:确定状态转移方程
f[i][0] = Σ(max (f[j][0], f[j][1]))
f[i][1] = 1+ Σf[j][0]
(j是i的儿子!!)
边界:f[i][0] = 0, f[i][1] = 1 ——–i是叶子节点
结果为max(f[root][0], f[root][1])
搜一下root然后dfs记忆化搜索
#include<iostream>#include<stdio.h>#include<string.h>#define input freopen("input.txt","r",stdin)using namespace std;int value[6002],pos[6002],v[6002];int dp[6002][2];int n;int find_root(){ int i; for(i=1;i<=n;i++) { if(pos[i]==i)return i; }}void dfs(int poss){ int i; v[poss]=1; for(i=1;i<=n;i++) { if(!v[i]&&pos[i]==poss) { dfs(i); dp[poss][0]+=max(dp[i][0],dp[i][1]); dp[poss][1]+=dp[i][0]; } }}int main(){ input; int i,j; int boss,emp,root; while(cin>>n) { memset(dp[0],0,sizeof(dp[0]));// memset(dp[1],1,sizeof(dp[1])); memset(v,0,sizeof(v)); for(i=1;i<=n;i++) { cin>>value[i]; } for(i=1;i<=n;i++) { pos[i]=i; dp[i][1]=value[i]; } for(i=1;i<=n;i++) { cin>>emp>>boss; if(emp==0&&boss==0)break; pos[emp]=boss; } root=find_root(); dfs(root); cout<<max(dp[root][0],dp[root][1])<<endl; } return 0;}
- POJ Anniversary party By Assassin
- poj 2342 Anniversary party
- POJ 2342 Anniversary party
- poj 2342 Anniversary party
- POJ 2342 Anniversary party
- POJ 2342 Anniversary party
- POJ-2342-Anniversary party
- POJ 2342 Anniversary party
- poj-2342Anniversary party
- POJ 2342 Anniversary party
- poj 2342 anniversary party
- POJ 2342 Anniversary party
- Anniversary party POJ - 2342
- Anniversary party POJ
- Anniversary party POJ
- poj 2342 Anniversary party
- POJ 2342 Anniversary party
- poj Anniversary party (树形DP)
- java浅说(6):Date类的使用
- 浮点型数据的存储方式
- 电脑系统维护 学习网站
- MYSQL第三弹(如果没记错)--正则表达式!
- C#字面值
- POJ Anniversary party By Assassin
- hdu 1814 和平委员会(2-sat入门题 字典序最小解)
- hdu 5115 Dire Wolf【区间dp】好题
- js中强制类型转换
- Android权限机制验证性实验
- 二分查找
- AOP入门
- xUtils
- 哈希表之开放地址的三种方法