poj2342 Anniversary party (树形dp)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9128   Accepted: 5250

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

Source

大致题意:

某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大。

思路:

思路:

任何一个点的取舍可以看作一种决策,那么状态就是在某个点取的时候或者不取的时候,以他为根的子树能有的最大活跃总值。分别可以用f[i,1]和f[i,0]表示第i个人来和不来。

上司来,下属不来

当i来的时候,dp[i][1] += dp[j][0];//j为i的下属

上司不来,下属来或不来

当i不来的时候,dp[i][0] +=max(dp[j][1],dp[j][0]);//j为i的下属

 #include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string> using namespace std; #define maxn 6005 int n;
int dp[maxn][],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
bool visited[maxn]; void tree_dp(int node)
{
int i;
visited[node] = ;
for(i=; i<=n; i++) //找1到n中找node节点的下属
{
if(!visited[i]&&father[i] == node)//i为下属
{
tree_dp(i);//递归调用孩子结点,从叶子结点开始dp
//关键
dp[node][] += dp[i][];//上司来,下属不来
dp[node][] +=max(dp[i][],dp[i][]);//上司不来,下属来、不来
}
}
} int main()
{
int i;
int f,c,root;
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(father,,sizeof(father));
memset(visited,,sizeof(visited));
for(i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
root = ;//记录父结点
bool beg = ;
while (scanf("%d %d",&c,&f),c||f)
{
//c的老爸是f
father[c] = f;
//儿子是跟节点 或者这条边是第一条边
//父亲做根节点
if( root == c || beg )
{
root = f;
}
}
//循环寻找根节点
while(father[root])//查找父结点
root=father[root];
//从根节点开始树形dp
tree_dp(root);
//取最大的上司去或者不去中的大值
int imax=max(dp[root][],dp[root][]);
printf("%d\n",imax);
}
return ; }

最新文章

  1. Indesign中GREP的应用
  2. Java 自动装箱、拆箱机制及部分源码分析
  3. (adhoc) process launch failed: timed out waiting for app to launch
  4. C# 查询Windows Service 信息 ,所在目录 启动状态
  5. hdu 1029
  6. 自主创建tcpdump/wireshark pcap文件
  7. 怎样制作PHP验证码?
  8. 一次项目中用到的php函数总结
  9. 当fixed元素相互嵌套时chrome下父元素会影响子元素的层叠关系
  10. VueJS 组件参数名命名方式和前台显示
  11. Bitmap.Config 说明 ALPHA_8 ARGB_4444 ARGB_8888 RGB_565
  12. 搭建mybatis时的小问题
  13. 关于if和else嵌套—蛋疼
  14. 使用R的注意事项
  15. golang使用redis
  16. IP地址分类以及子网划分
  17. SQL数据库优化
  18. idea操作快捷键
  19. Unity3D工程全资源自动检测系统
  20. Oracle GI 日志收集工具 - TFA

热门文章

  1. jQuery——插件制作
  2. JS——冒泡排序
  3. N的阶乘末尾有多少个零?
  4. Spring学习笔记_day01_ioc
  5. GeckoWebBrowser设置cookie
  6. IOS内购--后台PHP认证
  7. 微信小程序——weui的使用
  8. IO文件读取
  9. vivado2018.3 与 modelsim联合仿真
  10. BFS入门篇——RQNOJ195&amp;&amp;335