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 ...
随机推荐
- java8 Optional 类
package jdk180reduce; import java.util.ArrayList; import java.util.HashMap; import java.util.List; i ...
- 入门大数据---HiveCLI和Beeline命令行的基本使用
一.Hive CLI 1.1 Help 使用 hive -H 或者 hive --help 命令可以查看所有命令的帮助,显示如下: usage: hive -d,--define <key=va ...
- 一种基于LQR使输出更加稳定的算法(超级实用)
已知: 令: 则: 以上三式成立 具体步骤: 状态量最后一行加入“上一时刻的控制量”: A,B根据上述方法变形: Q,R增加维度(控制量一般都为一个,此时R维度不变): 最关键的是——输出量已经变为“ ...
- 理解与使用Javascript中的回调函数 -2
在javascript中回调函数非常重要,它们几乎无处不在.像其他更加传统的编程语言都有回调函数概念,但是非常奇怪的是,完完整整谈论回调函数的在线教程比较少,倒是有一堆关于call()和apply() ...
- Python之浅谈多态和封装
目录 组合 什么是组合 为什么使用组合 多态和多态性 多态 什么是多态? 多态性 好处 多态性 什么是多态性 封装 封装是什么意思? 隐藏 如何用代码实现隐藏 python 实际上是可以访问隐藏属性的 ...
- paramiko报错 Garbage packet received
前情概要 今天想要写一个多进程的python脚本上传代码至服务器,于是在本地用虚拟机测试了一下,可总是报错,具体报错信息如下 Traceback (most recent call last): Fi ...
- Centos 下 Jenkins2.6 + Git + Maven Shell一件部署与备份
使用Jenkins2.6 集成Maven与Git插件做持续集成,同时编写Shell脚本备份与发布(需要稍微知道点Linux/毕竟基于Centos PS:本人Linux也是菜鸡) - 下载Jenkins ...
- Mister B and PR Shifts,题解
题目链接 分析: 题意很明白,不再多说了,直接分析题目,首先想一想暴力,直接枚举起点,然后求出来,时间复杂度n*n,显然不太好,所以我们考虑换一种方法枚举,当然本质还是枚举,其实你会发现变化i次和i+ ...
- 洛谷 P2607 [ZJOI2008]骑士 树形DP
题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里, ...
- 查看windows操作系统的默认编码
转自:https://blog.csdn.net/zp357252539/article/details/79084480/ 在Windows平台下,进入DOS窗口,输入:chcp 可以得到操作系统的 ...