博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1520 Anniversary party
阅读量:4937 次
发布时间:2019-06-11

本文共 2994 字,大约阅读时间需要 9 分钟。

Problem 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 T 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

 

 

要举办一个聚会,每个人都编号从1开始,每个人有一个快乐度,整个单位等级可看成一棵树,只有一个根,每个人不与上级或下级同时参加,即父亲和儿子不同时参加聚会,参加聚会后获得此人相应的快乐度,求最大快乐度。

 


 

 

树形dp,开数组dp[N][2],dp[i][j]——第i个人参加或不参加,j=0不参加,j=1参加。数组存下以当前人为根,下面所有人最大快乐度。

输入后先找根,然后从根开始dfs到最低层从下向上传值。

答案就是根节点的值。

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #define MAX 7000 7 using namespace std; 8 int a[6666]; 9 int dp[6666][3];10 struct node11 {12 int to,nex;13 }edge[MAX*2+5];14 int head[MAX+5],cnt;15 void add(int u,int v)//添加一个单向边u->v 权为w 16 {17 edge[cnt].to=v;18 edge[cnt].nex=head[u];19 head[u]=cnt++;20 }21 int ru[6666];22 void dfs(int t)23 {24 int i;25 for(i=head[t];~i;i=edge[i].nex)26 {27 dfs(edge[i].to);28 }29 dp[t][0]=0;30 dp[t][1]=a[t];31 for(i=head[t];~i;i=edge[i].nex)32 {33 dp[t][1]+=dp[edge[i].to][0];34 dp[t][0]+=max(dp[edge[i].to][1],dp[edge[i].to][0]);35 }36 }37 int main()38 { 39 int n;40 while(cin>>n)41 { 42 memset(head,-1,sizeof(head));//初始化为-1 43 cnt=0;//初始化 044 memset(dp,0,sizeof(dp));45 memset(ru,0,sizeof(ru)); 46 int i;47 for(i=1;i<=n;i++)48 {49 scanf("%d",&a[i]);50 }51 int x,y;52 while(scanf("%d%d",&x,&y),x&&y)53 {54 add(y,x); 55 ru[x]++;56 }57 int root;58 for(i=1;i<=n;i++)59 {60 if(ru[i]==0)61 {62 root=i;63 break;64 }65 }66 dfs(root);67 cout<
<

  

转载于:https://www.cnblogs.com/jinmingyi/p/7306069.html

你可能感兴趣的文章
并查集
查看>>
11、组件注册-使用FactoryBean注册组件
查看>>
nyoj_95_众数问题_map练习
查看>>
uchome 是如何将数据插入数据库的
查看>>
For循环
查看>>
020-安装centos6.5后的生命历程
查看>>
面试介绍项目经验(转)
查看>>
创建并设置ASP.NET的会话状态服务器(SQL State Server)
查看>>
<metro>Google的验证
查看>>
SQL中NUMERIC和DECIMAL的区别
查看>>
安卓课程设计:微课表
查看>>
Oracle 表的分组操作
查看>>
在OS X上的Intllij Idea中配置GlassFish
查看>>
用查表法快速转换yv12到RGB【转】
查看>>
使用公钥登录SSL
查看>>
实验四 shell 编程(2)
查看>>
hdu 1290_献给杭电五十周年校庆的礼物
查看>>
Nginx 入门
查看>>
openCR-用ROS代码点亮LED的方法
查看>>
豆瓣电影api
查看>>