POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
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
Http
POJ:https://vjudge.net/problem/POJ-2342
HDU:https://vjudge.net/problem/HDU-1520
URAL:https://vjudge.net/problem/URAL-1039
Source
树型动态规划
题目大意
在一棵树上选取若干个点使得这些点均不互相相邻,且点权值之和最大。
解决思路
令F[i]表示以i为根节点的一棵子树的最大权值,F[i][0]表示不取i点的最大权值,F[i][1]表示取i点的最大权值。对于i的所有儿子节点j,则有F[i][0]=sum(max(F[j][1],F[j][0])) ,F[i][1]=sum(F[j][0])+Value[i];
有了状态转移方程,我们就可以用dfs的方式依次求解了。
需要注意的是,每个OJ的题面都没有表示有多组数据,POJ确实只有一组数据,而HDU是有多组数据的,在写的时候要注意。
这道题的加强版在这里(需要判断最优解是否唯一)
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxN=7000;
const int inf=2147483647;
int n;
int F[maxN][3];
int Happy[maxN];//每个点中的权值
vector<int> E[maxN];//存原图,里面的边是雇员->上司
vector<int> E2[maxN];//存返图,里面的边是上司->雇员
void dfs(int u);
int main()
{
while (cin>>n)
{
if (n==0)
break;
memset(F,0,sizeof(F));
for (int i=0;i<=n;i++)//因为有多组数据,所以每一次都要清空
{
E[i].clear();
E2[i].clear();
}
for (int i=1;i<=n;i++)
cin>>Happy[i];
int u,v;
while (cin>>u>>v)
{
if ((u==0)&&(v==0))
break;
E[u].push_back(v);
E2[v].push_back(u);
}
int start;
for (int i=1;i<=n;i++)
if (E[i].size()==0)
{
start=i;
break;
}
dfs(start);
cout<<max(F[start][1],F[start][0])<<endl;
}
return 0;
}
void dfs(int u)//dfs求F
{
for (int i=0;i<E2[u].size();i++)
{
int v=E2[u][i];
dfs(v);
F[u][0]=F[u][0]+max(F[v][0],F[v][1]);
F[u][1]=F[u][1]+F[v][0];
}
F[u][1]=F[u][1]+Happy[u];
return;
}
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)的更多相关文章
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...
- 树形DP URAL 1039 Anniversary Party
题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...
- POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...
- POJ 3398 Perfect Service(树型动态规划,最小支配集)
POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...
- POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法
POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...
- 树形dp(A - Anniversary party HDU - 1520 )
题目链接:https://cn.vjudge.net/contest/277955#problem/A 题目大意:略 具体思路:刚开始接触树形dp,说一下我对这个题的初步理解吧,首先,我们从根节点开始 ...
- Ural 1039 Anniversary Party
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1039 Dynamic Programming. 建立树形结构,每个employee有两个 ...
- DP Intro - poj 2342 Anniversary party
今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
随机推荐
- 01 json环境搭建
1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...
- 对pathtracing的一些个人理解
本人水平有限,若有错误也请指正~ 上面说到pathtracing(pt)的一些优点和缺点,优点即其实现很简单,这就是大概为什么当今市面上流行的很多渲染器如今都相继采用pathtracing算法为核心进 ...
- UVALive-5731
UVALive-5731 题意 一颗 n - 1 条边的有向树,要求整棵树成为强连通图,一次操作即构建一条路(一笔画), 限制: 新建的路上的所有边必须与原有的边逆向,即构建的路必须在原有的边和点上, ...
- android Activity Application Context 的区别
用了这么久的Context,对于Context究竟是个什么玩意一直不是很明白.看了网上十几篇文章的介绍 加上自己的理解总结一下.(自己想法,不对勿喷,不想照搬网上一些文字说法来糊弄自己,自己理解的就这 ...
- 关于MS12-020一次简单尝试
由于之前着重于web漏洞,主机漏洞这块比较薄弱.也没有用过metasploit,对于很多系统漏洞还不熟悉,正好这几天不忙,就想着慢慢学习,再写点简单的东西,进行总结记录. 这次尝试的是MS12-020 ...
- [转]tomcat部署(1)
阅读目录 1 目录结构 2 部署 3 发布 4 测试 本篇参考Tomcat官方文档:<First Webapp>翻译,并结合自己的开发经验介绍关于tomcat部署以及发布的相关内容. ...
- 做电子商务的七个SEO技巧
随着电子商务网站的迅速增加,为了适应不断增长的网络客户多样化需求.越来越多的人注意到,从网站上购买既节省了时间和金钱,因此电子商务社区越来越激烈.网站需要具有独特的设计风格和功能,而不是网站内容的不断 ...
- Spring学习(12)--- @Autowired与@Resource 对比
Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource. @PostConstruct及@PreDestroy. 1. @Autowi ...
- 第一个CGI程序-----完全就是普通的c语言嘛‘(*∩_∩*)′
第一个CGI程序 ----完全就是普通的C语言嘛 '(*∩_∩*)′ PainterQ 2017年5月14日 上一篇博文里面叙述了Apache的安装和配置方法,恍恍惚惚我就拥有了自己的第一个http ...
- [Open Source] 负载均衡之Nginx
目录 简介 常用命令 功能 代理 反向代理 集群 HTTP集群 TCP集群 重定向 静态文件 HTTPS配置 常见问题 简介 Nginx ("engine x") 是一款轻量级,高 ...