Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6990    Accepted Submission(s): 3104

Problem 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 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
 
Source

题意: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入门)的更多相关文章

  1. [poj2342]Anniversary party树形dp入门

    题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...

  2. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  3. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  4. poj 2342 Anniversary party 树形DP入门

    题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...

  5. (树形DP入门题)Anniversary party(没有上司的舞会) HDU - 1520

    题意: 有个公司要举行一场晚会.为了让到会的每个人不受他的直接上司约束而能玩得开心,公司领导决定:如果邀请了某个人,那么一定不会再邀请他的直接的上司,但该人的上司的上司,上司的上司的上司等都可以邀请. ...

  6. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  7. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  8. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  9. 树形DP入门详解+题目推荐

    树形DP.这是个什么东西?为什么叫这个名字?跟其他DP有什么区别? 相信很多初学者在刚刚接触一种新思想的时候都会有这种问题. 没错,树形DP准确的说是一种DP的思想,将DP建立在树状结构的基础上. 既 ...

随机推荐

  1. react中使用屏保

    1,默认路由路径为屏保组件 <HashRouter history={hashHistory}> <Switch> <Route exact path="/&q ...

  2. cycloneii normal mode vs. arithmetic mode

    Combout = F(data1, data2, data3 or cin, data4) 在 arithmetic mode,只有 data1\data2 参加运算. 4-LUT到2个3-LUT的 ...

  3. thinkphp 数据创建

    在进行数据操作之前,我们往往需要手动创建需要的数据,例如对于提交的表单数据: // 获取表单的POST数据 $data['name'] = $_POST['name']; $data['email'] ...

  4. 期望——邮票收集问题lightoj1342

    邮票手机问题: 有n种类型的邮票,问将所有的类型的邮票全部收集起来所要的收集次数期望是多少. 设dp[i]为已经收集了i种类型的票,还要收集n-i种的次数的期望. dp[n]=0; 递推式: dp[i ...

  5. Ubuntu安装Windows软件

    https://www.cnblogs.com/chendeqiang/p/10177530.html Windows系列软件 安装Deepin封装好的框架 git clone https://git ...

  6. c语言学习笔记 - 结构体位域

    在学习结构体的时候遇到了位域这个概念,位域主要是为了节省内存空间,比如用一个32位,4个字节的int存储一个开关变量时,会造成空间浪费,于是干脆就考虑在这个32划分不同的区域来存储数据,例如划出1位存 ...

  7. JDK源码阅读--StringBuffer

    public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharS ...

  8. 01-python 学习第一天

    今天是学习python语言的第一天,通过中国大学MOOC网的相关课程和其他在线教程,对python 语言有了初步的认识.今天我记住了<人生苦短,Python是岸>这句话.如果有人学习,建议 ...

  9. history配置

    /etc/profile配置文件中,末尾增加如下参数项: HFILE=`who -m | awk '{print $1}'`readonly HISTFILE=/var/history/$HFILE- ...

  10. BMP 图片格式

     BMP根据颜色深度,可以分为2(1位).16(4位).256(8位).65536(16位)和1670万(24位)以及32位含有alpha通道.8位图像可以是 索引彩色图像外,也可以是灰阶图像,而索引 ...