Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3862   Accepted: 2171

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

【题意】

公司有n个人,每个人有价值vi,有一天举办年会,每个人都可以参加,但有严格的等级制度,参加活动时,不能同时出现a和a的上司,问如何才能使总和最大。

【分析】

每个人只有去和不去两种状态,设DP[i][0]和DP[i][1]分别表示第i个人不参加和参加年会,获得的总的最大价值。

则状态转移方程为:

    DP[i][1] += DP[j][0],

    DP[i][0] += max{DP[j][0],DP[j][1]};其中j为i的孩子节点。

    这样,从根节点r进行dfs,最后结果为max{DP[r][0],DP[r][1]}。

(分析来自yzmduncan

第2~n+1行为这n个人的价值

代码:

 #include "stdio.h"   //简单的树形dp题
#include "string.h"
#include "queue"
using namespace std; #define N 60005
#define INF 0x3fffffff struct node
{
int x,y;
int weight;
int next;
}edge[*N];
int idx,head[N]; int root;
int du[N];
int value[N]; int dp[N][];
int MAX(int a,int b) { return a>b?a:b; } void Init()
{
idx = ;
memset(head,-,sizeof(head));
} void Add(int x,int y,int weight)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = weight;
edge[idx].next = head[x];
head[x] = idx++;
} void DFS(int i) //
{
int k,j;
dp[i][] = ;
dp[i][] = value[i];
for(k=head[i]; k!=-; k=edge[k].next)
{
j = edge[k].y;
DFS(j);
dp[i][] += MAX(dp[j][],dp[j][]);
dp[i][] += dp[j][];
}
} int main()
{
int n;
int i;
int x,y;
while(scanf("%d",&n)!=EOF)
{
Init();
memset(du,,sizeof(du)); //记录节点的入度
memset(dp,,sizeof(dp));
for(i=; i<=n; ++i)
scanf("%d",&value[i]);
while(scanf("%d %d",&x,&y) && x+y>)
{
Add(y,x,);
du[x]++;
}
for(i=; i<=n; ++i)
{
if(du[i]==)
root = i;
}
DFS(root);
printf("%d\n",MAX(dp[root][],dp[root][]));
}
return ;
}
												

最新文章

  1. Bullet的学习资源(用Doxygen生成API文档)
  2. LINQ to SQL语句(4)之Join
  3. 浅谈JavaScript中的Ajax
  4. org.apache.hadoop.hbase.TableExistsException: hbase:namespace
  5. Android应用程序请求SurfaceFlinger服务渲染Surface的过程分析
  6. VS2010 C#调用C++ DLL文件
  7. java 以a为开头单词的词典查询示例
  8. How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
  9. Python笔记之数据类型
  10. 如何修改const常量值
  11. GET和POST有什么区别?及为什么网上多数答案都是错的(转载)
  12. poj2987 求最大权闭合回路
  13. leetcode AC1 感受
  14. Android Studio 修改Logcat的颜色
  15. memcached 连接本地问题
  16. nginx keepalived 高可用方案(转)
  17. Kubernetes1.3:POD生命周期管理
  18. centos7 ping: www.baidu.com: Name or service not known
  19. tomcat配置JMX
  20. 使用经验风险最小化ERM方法来估计模型误差 开坑

热门文章

  1. Use the PDFs below or the HTML contents to the left to install and configure P6 EPPM and its additional components.
  2. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
  3. 根据Expander的IsExpanded属性值的变化动态设计Control的size
  4. csharp: Export or Import excel using MyXls,Spire.Xls
  5. Run python as a daemon process
  6. 如何高效部署前端代码,如css,js...
  7. struts2进阶篇(4)
  8. mybatis generator with oracle
  9. 关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案
  10. Runtime -----那些被忽略的技能