传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1520

Anniversary party

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

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
Ural State University Internal Contest October'2000 Students Session
Recommend
linle   |   We have carefully selected several similar problems for you:  1561 1011 2196 1494 2242 
题目大意:

学校要开一个聚会。学校的教职工之间有上下级关系,为了让所有人开心,宴会组织者决定不会同时邀请一个人和他的上级(这让我想起我们昨天晚上聚餐李晔老师不来,她怕她来了我们放不开。。。。),对于每一个人,他能给聚会带来的欢乐度有一个值,问组织者该邀请哪些人能够使宴会的欢乐度达到最大值。
解题思路:
首先是DP的部分(也是很无聊的一部分):每个参与者都有两种状态,一种是参加,一种是不参加。这个状态的后续影响就是如果他参加了,他的直接上司和直接下属都不能参加。我们可以用一个二维二态的数组来描述:dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加。状态转移方程就是dp[i][1]=dp[i][1]+dp[i-1][0],dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1])。
本质:将一个数字三角形的操作放在了树上
第一道树形dp题,纪念一下,虽然对树的部分理解的还不是很透彻
code:
#include <iostream>
#include <stdio.h>
#include<memory>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
#define max_v 6005
struct node
{int pa,son;int next;
} point[max_v];int dp[max_v][2];
//dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加
//状态转移方程就:
//dp[i][1]=dp[i][1]+dp[i-1][0]
//dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1])int List[max_v];int vis[max_v];//vis[a]=1 表示a有父节点int value[max_v];//存值
int pos;void add(int pa,int son)
{point[pos].pa=pa;point[pos].son=son;point[pos].next=List[pa];List[pa]=pos++;
}void dfs(int root)
{if(List[root]==-1)//root没有子节点了
    {dp[root][1]=value[root];dp[root][0]=0;return ;}int now=List[root];dp[root][0]=0;dp[root][1]=value[root];while(now!=-1){dfs(point[now].son);dp[root][1]+=dp[point[now].son][0];//既然取了父节点的值,子节点的值就不能再取了。//父节点的值没有取,子节点的值分取和不取两种情况,取其中较大的那种情况。dp[root][0]+=max(dp[point[now].son][1],dp[point[now].son][0]);now=point[now].next;//这个子节点计算过了,就要开始计算下一个子节点了
    }return ;
}int main()
{int n;while(cin>>n){for(int i=1; i<=n; i++)cin>>value[i];//记录每一个点的值
memset(List,-1,sizeof(List));memset(vis,0,sizeof(vis));int a,b;pos=0;while(~scanf("%d %d",&a,&b)){if(a==0&&b==0)break;add(b,a);  //将边加入树中vis[a]=1; //记录a有父节点,不可能是祖节点。
        }a=1;while(vis[a]==1)a++;//找到根结点
dfs(a);//从根结点开始搜
printf("%d\n",max(dp[a][0],dp[a][1]));//取最大
    }return 0;
}

转载于:https://www.cnblogs.com/yinbiao/p/9406533.html

hdu 1520 Anniversary party(第一道树形dp)相关推荐

  1. 蓝桥杯节点选择(java)第一道树形dp分析

    蓝桥杯 节点选择 问题描述 有一棵 n 个节点的树,树上每个节点都有一个正整数权值.如果一个点被选择了,那么在树上和它相邻的点都不能被选择.求选出的点的权值和最大是多少? 输入格式 第一行包含一个整数 ...

  2. HDU 1520 Anniversary party(树形dp)

    HDU 1520 Anniversary party(树形dp) 树形dp第一题!!! 题意很清晰,思路也很明确.很容易找到根节点,即最大的boss,通过根节点向下dp. 状态转移方程: int to ...

  3. HDU - 1520 Anniversary party [树形dp]

    Anniversary party 时限:1000ms Problem Description There is going to be a party to celebrate the 80-th ...

  4. POJ 2342 | HDU 1520 Anniversary party 树形DP(入门题)

    传送门:POJ 2342 题目大意: 有若干人参加一个聚会,如果两个人之间有直接的上下属关系,则只能去一个.每个人都有个高兴值,问高兴值之和最大是多少? 思路: 之前一直觉得树形DP比较难,现在发现树 ...

  5. hdu 1561 The more, The Better 树形dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1561 做树形dp比较小. 先上网学习下,总结下套路. dp[i][j]表示在第i个节点,有j个名额选的时候的最大 ...

  6. HDU 4123 Bob’s Race(树形DP+RMQ)

     题目大意:给定一棵树,每一个点都从当前位置走到距离最远的位置,1~n的连续区间中最大并且走的最远距离差值不超过Q的区间右多大. 思路:首先求出每个点能到达的最远距离,这一步有两种做法: 第一种做 ...

  7. HDU - 6769-In Search of Gold-二分+树形dp

    https://vjudge.net/problem/HDU-6769 题目大意:给你n个点,有n-1条边,每条边有a,b两个权值,给你一个k,恰好有k条边的权值取a,其余取b的时候,树的直径的最小值 ...

  8. 【HDU - 5452】Minimum Cut(树形dp 或 最近公共祖先lca+树上差分,转化tricks,思维)

    题干: Given a simple unweighted graph GG (an undirected graph containing no loops nor multiple edges) ...

  9. hdu 4799 LIKE vs CANDLE(树形dp)

    题目链接:hdu 4799 LIKE vs CANDLE 解题思路 dp[i][0]表示第i个节点不翻转的最优解,dp[i][1]表示翻转的最优解,每个位置翻转的代价到其父亲节点的位置再计算. 代码 ...

最新文章

  1. 计算机专业每年都有国企招老吗,这十大专业在国企中最受欢迎,待遇高、前景好,有你的专业吗?...
  2. 社区 正式发布了跨平台的 CoreWCF 0.2.0
  3. python的多行语句可以使用反斜杠_python 为什么不用分号作终止符?
  4. bcache使用教程
  5. 抽象编程语言(APL)是什么?
  6. Linux内核驱动调试,Linux内核设备驱动之内核的调试技术笔记整理
  7. jQuery LigerUI 初次发布一睹为快(提供Demo下载)
  8. 最全eclipse自由轻松来回切换项目的jdk版本的方法
  9. 深度学习系列(一)【人类语言处理--引言】
  10. Windows勒索病毒'Wannacry'修复补丁下载,救命必看!
  11. 1.概率论-组合分析
  12. react hooks子组件向父组件传参
  13. 【Books系列】席慕蓉《回眸》欣赏
  14. 【机房收费系统个人版】三层登陆
  15. 从词嵌入到文档距离论文笔记(From Word Embeddings To Document Distances)
  16. Hadoop3.2.1 【 YARN 】源码分析 : LinuxContainerExecutor 浅析 [ 一 ]
  17. 车载业务连续亏损,广和通并购标的净利率持续下滑
  18. 仿朋友圈相册图片选择以及画廊效果
  19. winds搭建bugfree环境
  20. 目标检测算法——图像分类开源数据集汇总(附下载链接)

热门文章

  1. ftp 访问mysql6,RedHat6.5开启ftp访问已有目录方法
  2. linux 重复模式元字符,Linux 正则表达式 vi, grep, sed, awk
  3. linux arm9开发环境,ARM9开发板Qt环境的搭建
  4. win10 安装db2 10.1 并使用DBserver连接db2数据库
  5. js压缩代码后怎么生成source map_浅谈前端代码加密
  6. java jvm 查看内存_JVM内存状况查看方法和分析工具(下)
  7. mysql objectid_MongoDB 的objectid和UUID
  8. python撤销_python 取消转义字符作用
  9. 运筹优化(三)--线性规划之单纯形法
  10. NAT-PT (Network Address Translation-Protocol)网络地址转换协议转换