HDU1520 Anniversary party —— 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12366 Accepted Submission(s): 5009
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.
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
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
5
题解:
dp[u][state]:结点u其状态为state(0或1,即不放和放)的最大值。
状态转移:
1.如果当前结点不放,那么它的子结点放不放都行,所以两者取其大。
2.如果当前结点放,那么它的子节点只能不放。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <cmath>
- using namespace std;
- typedef long long LL;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+7;
- const int MAXN = 1e4+10;
- int val[MAXN], hav_fa[MAXN], dp[MAXN][2];
- vector<int>son[MAXN];
- int dfs(int u)
- {
- dp[u][0] = 0; //不放,初始化为0
- dp[u][1] = val[u]; //放,初始化为本身的值
- for(int i = 0; i<son[u].size(); i++)
- {
- int v = son[u][i];
- dfs(v); //递归其子树
- dp[u][0] += max(dp[v][0], dp[v][1]); //如果不放,那么它的子节点放不放都行,所以取其大。
- dp[u][1] += dp[v][0]; //如果放,那么它的子节点不能放
- }
- return max(dp[u][0], dp[u][1]); //返回最大值
- }
- int main()
- {
- int n;
- while(scanf("%d",&n)!=EOF)
- {
- for(int i = 1; i<=n; i++)
- son[i].clear(), hav_fa[i] = 0;
- for(int i = 1; i<=n; i++)
- scanf("%d",&val[i]);
- int u, v;
- while(scanf("%d%d",&v,&u) && (v || u))
- {
- son[u].push_back(v);
- hav_fa[v] = 1;
- }
- for(int i = 1; i<=n; i++) //找到根节点,然后深搜
- if(!hav_fa[i])
- cout<< dfs(i) <<endl;
- }
- }
HDU1520 Anniversary party —— 树形DP的更多相关文章
- HDU1520 Anniversary party 树形DP基础
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...
- hdu1520 Anniversary party (树形dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- hdu1520 第一道树形DP,激动哇咔咔!
A - 树形dp Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- [poj2342]Anniversary party_树形dp
Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
随机推荐
- uva 1426 离散平方根
1426 - Discrete Square Roots Time limit: 3.000 seconds A square root of a number x <tex2html_verb ...
- 洛谷 [P3150] pb的游戏
博弈论基础 本题可以视作P2148 E&D 的前置技能 本题直接判断奇偶性来求解, 证明就是2148 的证明 不贴代码
- Ubuntu MySQL的安装使用
删除 mysql sudo apt-get autoremove --purge mysql-server-5.0 sudo apt-get remove mysql-server sudo apt- ...
- POJ3345 Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5021 Accepted: 1574 Description There ...
- AC日记——租用游艇 洛谷 P1359
题目描述 长江游艇俱乐部在长江上设置了n 个游艇出租站1,2,…,n.游客可在这些游艇出租站租用游艇,并在下游的任何一个游艇出租站归还游艇.游艇出租站i 到游艇出租站j 之间的租金为r(i,j),1& ...
- 《Java虚拟机原理图解》 1.2.2、Class文件中的常量池详解(上)
我的上一篇文章<Java虚拟机原理图解> 1.class文件基本组织结构中已经提到了class的文件结构,在class文件中的魔数.副版本号.主版本之后,紧接着就是常量池的数据区域了,如下 ...
- 【SQL Server 学习系列】-- 获取字符串中出现某字符的次数及字符某次出现的下标
) = '1_BB_CC_DD_AA_EE_YY_WW_HH_GG' --// 1. 获取下划线在字符串中出现的次数 SELECT LEN(@Str) - LEN(REPLACE(@Str, '_', ...
- iOS release版本去除NSLog打印信息
因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...
- kafka的安装和使用;kafka常用操作命令
kafka:基于发布/订阅的分布式消息系统.数据管道:最初用来记录活动数据--包括页面访问量(Page View).被查看内容方面的信息以及搜索情况等内容和运营数据--服务器的性能数据(CPU.IO使 ...
- BUPT复试专题—找K小数(2011)
https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd?tpId=67&tqId=29637&rp=0&a ...