AYITACM2016省赛第三周 L - Anniversary party(树形dp)
来源:互联网 发布:eve1200炮数据 编辑:程序博客网 时间:2024/06/08 12:32
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
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
711111111 32 36 47 44 53 50 0
Sample Output
5
题意:
n个人(编号1-N),然后n行分表代表第n个人的活跃度,之后若干行 L 和 K(0 0结束),代表K是L的上司,
问一个聚会中邀请这n个人中的若干,其中不能含直接的上下级关系,可以使聚会中的活跃度最大为多少。
状态转移:
上司去,下级不去:
dp[c][1]+=dp[t][0]; //c去,则t必不能去
上司不去,下级去或者不去:
dp[c][0]+=max(dp[t][0],dp[t][1]); //c不去,取t去或不去的最大值
和深搜很像,也就是用的深搜。
#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;vector<int>g[10001];int dp[10001][3],root[10001];void dfs(int c){ int i,t; for(i=0;i<(int)g[c].size();i++) { t=g[c][i];//节点的孩子 dfs(t);//把孩子作为节点再搜索它的孩子 dp[c][1]+=dp[t][0];//如果双亲去,孩子就不能去 dp[c][0]+=max(dp[t][0],dp[t][1]);//如果双亲不去,就选择孩子中欢乐值大的去 }}int main(){ int n,i,a,b; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { scanf("%d",&dp[i][1]);//输入第i个人的活跃度 root[i]=i;//初始化每个节点都是根节点,也是并查集的初始化 g[i].clear();//清空容器 } while(scanf("%d%d",&a,&b)&&a+b) { g[b].push_back(a);//把数据存入容器中 root[a]=b; //a的根节点为b } int beg; for(i=1;i<=n;i++) if(root[i]==i)//如果根节点是本身,说明就是树的根节点 { beg=i; break; } dfs(beg);//搜索这棵树 printf("%d\n",max(dp[beg][0],dp[beg][1]));//输出最终的欢乐值 }return 0;}
0 0
- AYITACM2016省赛第三周 L - Anniversary party(树形dp)
- Anniversary party(树形dp)
- Anniversary party(树形DP)
- Anniversary party(树形DP)
- Anniversary party (树形dp)
- 【树形DP】Anniversary party
- Anniversary party--树形DP
- Anniversary Party(树形dp)
- 树形dp--Anniversary party
- 树形DP- Anniversary party
- hdu(1520) Anniversary party(树形dp)
- poj 2342 Anniversary Party(树形dp)
- POJ 2342 Anniversary party(树形DP)
- hdu 1520 Anniversary party(树形dp)
- 树形dp Anniversary party(HDU1520)
- poj 2342Anniversary party(树形dp)
- Anniversary party(树形DP入门)
- poj 2342 Anniversary party(树形dp)
- Apache-dbutils 简介及事务处理
- 解决 googel 无法直接跳转网页打开搜索结果
- 移动端破解TLS加密的数据的设计思路
- 画虚线
- Spring MVC获取ApplicationContext
- AYITACM2016省赛第三周 L - Anniversary party(树形dp)
- Apue学习:File I/O
- Android ORMLite 框架的入门用法
- ubuntu一些基本软件安装方法
- c++ 链接mysql:error LNK2019: 无法解析的外部符号
- 近期工作中遇到的问题及其解决办法
- WIN7更改用户名访问共享文件夹
- log4j.properties 详解与配置步骤
- Baidu的正确打开方式