题目链接:http://poj.org/problem?id=2342

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的水题。

状态转移方程:

设dp[i][0]为第i号人不去的情况下,以其为根的子树,最大的rating和;dp[i][1]为第i号人去的情况下,以其为根的子树,最大的rating和;

对于树上的每条边:edge[u][ (v[1]) ]……edge[u][ (v[k]) ]……,都有:

  dp[u][0]+=∑ max( dp[ (v[k]) ][1] , dp[ (v[k]) ][0] )

  dp[u][1]+=∑ dp[ (v[k]) ][0]

将每个人的dp[i][1]按照输入初始化后,用vector记录下一个点的所有孩子,并且找到root,最后进行DFS即可。

 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define MAXN 6005
using namespace std;
int n,fl[MAXN],dp[MAXN][],root;
vector<int> child[MAXN];
void dfs(int now)
{
int next;
for(int i=;i<child[now].size();i++)
{
next=child[now][i];
dfs(next);
dp[now][]+=max(dp[next][],dp[next][]);
dp[now][]+=dp[next][];
}
}
int main()
{
memset(fl,,sizeof(fl)); scanf("%d%",&n); for(int i=;i<=n;i++) scanf("%d",&dp[i][]);
for(int i=;i<=n;i++)
{
int L,K;
scanf("%d%d",&L,&K);
child[K].push_back(L);
fl[L]=;
}
for(int i=;i<=n;i++) if(fl[i]==){root=i;break;} dfs(root);
printf("%d\n",max(dp[root][],dp[root][]));
}

最新文章

  1. idea缓存
  2. rewrite规则写法及nginx配置location总结
  3. linux下安装rzsz
  4. 数据结构与算法分析&ndash;Minimum Spanning Tree(最小生成树)
  5. selenium操作浏览器cookie方法
  6. Codeforces Round #379 (Div. 2) 解题报告
  7. 自定义控件EditText
  8. poj 2031 Building a Space Station(最小生成树,三维,基础)
  9. KnockoutJS(2)-监控属性
  10. Java开发者易犯错误Top10
  11. 智能卡安全机制比较系列(六) TimeCOS
  12. salesforce 零基础学习(七十)使用jquery table实现树形结构模式
  13. Oracle解析复杂json的方法
  14. Tomcat 热部署
  15. java基础,集合,Arraylist,源码解析(基础)
  16. [Swift-2019力扣杯春季决赛]2. 按字典序排列最小的等效字符串
  17. HashMap源码解读(jdk1.8)
  18. [十九]JavaIO之PipedReader 和 PipedWriter
  19. 7A - Max Sum
  20. 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)

热门文章

  1. iOS开发之-- 字符串的操作,去掉某一个字符或者替换成其他字符
  2. java中类相关注意事项
  3. java的代理和动态代理简单测试
  4. Splash resource_timeout 属性
  5. codeforces水题100道 第十一题 Codeforces Round #143 (Div. 2) A. Team (brute force)
  6. PHP错误 。Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_ENDSWITCH or T_CASE or T_DEFAULT
  7. 页面调用Iframe中数据
  8. epoll实现机制分析
  9. 基于ThinkPHP3.23的简单ajax登陆案例
  10. IOS设计模式第五篇之装饰设计模式的代理设计模式