hdu oj 1520 Anniversary party(树形dp入门)
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6990 Accepted Submission(s): 3104Problem DescriptionThere 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.InputEmployees 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 0OutputOutput should contain the maximal sum of guests' ratings.Sample Input711111111 32 36 47 44 53 50 0Sample Output5Source
题意:n个人(编号1-N),然后n行分表代表第n个人的活跃度,之后若干行 L 和 K(0 0结束),代表K是L的上司,
问一个聚会中邀请这n个人中的若干,其中不能含直接的上下级关系,可以使聚会中的活跃度最大为多少。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
const int N = ;
int dp[N][],root[N];
vector<int>G[N];
void DFS(int cur)
{
for(int i = ; i < (int)G[cur].size(); i++)
{
int to = G[cur][i];
DFS(to);
dp[cur][] += dp[to][];//该级去,下一级不去
dp[cur][] += max(dp[to][],dp[to][]);//该级不去,下级去不去选最优
}
} int main(void)
{
int n;
while(scanf("%d",&n) != EOF)
{
memset(dp,,sizeof(dp));
for(int i = ; i <= n; i++)
{
scanf("%d",&dp[i][]);//代表第几个人去能增加的活跃度
root[i] = i;//并查集的初始化
G[i].clear();
}
int a,b;
while(scanf("%d %d",&a,&b),a != ||b != )
{
G[b].push_back(a);//vector建图
root[a] = b;
}
int beg;
for(int i = ; i <= n; i++)
{
if(root[i] == i)//查找根节点
{
beg = i;
break;
}
}
DFS(beg);
printf("%d\n",max(dp[beg][],dp[beg][]));
}
return ;
}
hdu oj 1520 Anniversary party(树形dp入门)的更多相关文章
- [poj2342]Anniversary party树形dp入门
题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- poj 2342 Anniversary party 树形DP入门
题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...
- (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520
题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...
- 树形dp 入门
今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...
- POJ 2342 树形DP入门题
有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 树形DP入门详解+题目推荐
树形DP.这是个什么东西?为什么叫这个名字?跟其他DP有什么区别? 相信很多初学者在刚刚接触一种新思想的时候都会有这种问题. 没错,树形DP准确的说是一种DP的思想,将DP建立在树状结构的基础上. 既 ...
随机推荐
- csp-s模拟测试56Merchant, Equation,Rectangle题解
题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...
- 用JS写的一个简单的时钟
没什么技术含量,单纯的想传上去.手痒了 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- Android之FrameLayout帧布局
1.简介 这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角; 帧布局的大小由控件中最大的子控件决定,如果控件的大小一样大的话,那么同一时刻就只能看 ...
- JAVA基础_可变参数
自JAVA1.5以来,在JAVA中出现了可变参数一说,其针对的情况是对多个不确定的相同类型的元素进行同一类操作的情形. 可变参数有点类似与重载的概念,但是其中的可变参数是被隐式的转换成数组来进行处理的 ...
- 一起使用Python里for循环和dictionary字典
1.先定义一个字典的内容 i= { 'status': 'success', 'country': '中国', 'countryCode': 'CN', 'region': 'BJ' } 2.打印字典 ...
- 03_Hibernate关系映射
关系映射? Hibernate的主要目的就是JAVA程序员可以随心所欲的使用对象编程思维来操作数据库.一些数据库表的关系我们应该可以通过hibernate实现,比如数据库中用到的主外键关系,还有一些与 ...
- <每日一题>题目13:列表的简单问题
''' 分析: python赋值是通过指针来进行的. 很显然第一.三.四次调用都指向同一个列表,并未完成清空, 第二次调用只是指向了另一个列表,也未完成清空,很显然结果是累计的 结果: [0, 1] ...
- C# 判断当前请求是GET、还是POST ?
方法一: HttpContext.Current.Request.RequestType == "POST" //当前请求为:POST 方法二: if(Request.Serv ...
- 解决Navicat 报错:1130-host is not allowed MySQL不允许从远程访问的方法
ERROR 1130: Host '192.168.1.3' is not allowed to connect to thisMySQL server 解决方法: 1. 改表法.可能是你的帐号不允许 ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...