HDU - 1520 Anniversary party (树的最大独立集)
Time limit :1000 ms ;Memory limit :32768 kB; OS :Windows
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
题意:给你一棵树,你从中选择若干结点,要求这些结点中任意两个不能有“父子”关系,求最后权值的最大值。
分析:这便是紫书上所说的“树的最大独立集”问题,这要是放在线性表上,直接用01背包就解决了,但树上的动态规划就是把这种简单操作放在树上解决,难度就增加了。解题步骤如下:
①用邻接表储存所给的树。
②因为每个点可取可不取,dp[i][0]表示第i个结点不选时的最大值,dp[i][1]表示第i个结点选时的最大值。状态转移方程为: dp[father][0] += max(dp[son][0],dp[son][1]);父亲没有被邀请 ,dp[father][1] += dp[son][0];父亲被邀请了,儿子不能被邀请。
③最终结果为max(dp[root][1],dp[root][0])。
AC代码如下(93ms):
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define _for(i,a,b) for(int i = a;i < b;i++)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define all(s) s.begin(), s.end()
const int maxn = 6050;
int N;
bool no_root[maxn];//判断是不是树根
vector<int>G[maxn];//邻接表
int dp[maxn][2];//dp[i][0]表示以i为根的子树,不选i时的最大值
//dp[i][1]表示以i为根的子树,选i时的最大值
void dfs(int root)
{
int nson = G[root].size();
_for(i, 0, nson)
{
int son = G[root][i];
dfs(son);
dp[root][0] += max(dp[son][1], dp[son][0]);
dp[root][1] += dp[son][0];
}
}
int main()
{
while (scanf("%d", &N) == 1)
{
memset(no_root, 0, sizeof(no_root));
rep(i, 1, N)G[i].clear();//清空邻接表
rep(i, 1, N)
{
scanf("%d", &dp[i][1]);
dp[i][0] = 0;
}
int v, u;//员工与直属上级
while (scanf("%d%d", &v, &u) == 2 && v + u)//u是v的父结点
{
G[u].push_back(v);
no_root[v] = true;//v不是根
}
int ans = 0;
rep(i, 1, N)
{
if (no_root[i] == 0)//从根结点开始(注意,这里可能不止一个根)
{
dfs(i);
ans = max(ans, max(dp[i][0], dp[i][1]));
}
}
printf("%d\n", ans);
}
return 0;
}
HDU - 1520 Anniversary party (树的最大独立集)的更多相关文章
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- 题解报告:hdu 1520 Anniversary party(树形dp入门)
Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...
- hdu 1520 Anniversary party 基础树dp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1520.Anniversary party 基础的树形dp
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个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu 1520 Anniversary party || codevs 1380 树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- [HDU 1520] Anniversary party
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- vue-drag-resize 可拖拽可缩放的标签,如何管理多个拖拽元素之间的zIndex?操作上需要保持当前激活的组件是最上层,但是在总体上,又要确保其图层管理的顺序。
麻烦总是不断出现,还好办法总比困难多, 1.公司开发一款可视化编辑html网页的多媒体编辑平台,牵扯到标签元素的拖拽,缩放,我找了找方法发现原生技术实现起来代码太多,麻烦,还好找到了一个vue组件,可 ...
- Redis - (Linux)安装与配置
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: 1:Redis支持数据的持久化,可以将内存 ...
- InnoDB 中 B+ 树索引的分裂
数据库中B+树索引的分裂并不总是从页的中间记录开始,这样可能会导致空间的浪费,例如下面的记录: 1, 2, 3, 4, 5, 6, 7, 8, 9 插入式根据自增顺序进行的,若这时插入10这条记录后需 ...
- 尚硅谷 dubbo学习视频
1 1.搭建zookpeer注册中心 windows下载zooker 需要修改下zoo_sample .cfg为zoo.cnf 然后需要zoo.cnf中数据文件的路径 第五步:把zoo_sample ...
- IDEA2019版中文汉化包
废话不多说,上才艺 E G M~~~~~ 2020版的IDEA大佬可以无视........ 1.打开IDEA文件目录 2.打开lib目录--将汉化版复制到该目录下 3.打开IDEA查看效果 高铁链 ...
- MongoDB快速入门教程 (1)
1.MongoDB初识 1.1.MongoDB是什么? MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于 ...
- Nginx配置upstream并且实现负载均衡
感谢看过这一些列博文和评论的小伙伴, 我把自己所看到的学到的拿到这里来分享是想和大家一起学习进步, 想听听园友给出的意见, 也是对自己学习过程的一个总结. 技术无止境, 我们仍需努力! 1,话不多说, ...
- C# 获取枚举的描述Description
方法类: public static class EnumExtensions { #region Enum /// <summary> /// 获取枚举变量值的 Description ...
- 在web开发中,为什么前端比后端更得到转行程序员的青睐?必看!
1.Web开发分类与区别 人们通常将Web分为前端和后端,前端相关的职位有前端设计师(UI/UE),前端开发工程师,后端相关的有后端开发工程师. 2.技术栈区别 看各大招聘网站上,公司对前端开发工程师 ...
- Python3笔记016 - 4.1 序列
第4章 序列的应用 python的数据类型分为:空类型.布尔类型.数字类型.字节类型.字符串类型.元组类型.列表类型.字典类型.集合类型 在python中序列是一块用于存放多个值的连续内存空间. py ...