题干:

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

解题报告:

树形dp解决,话说竟然开2e7的数组还不炸、、

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int head[6000 + 5],in[6000 +5];
int val[6000 + 5];
int dp[6000 +5][2];//0代表不选; 1代表选
int top = 0;
struct Node {int to;int ne;
} e[20000005];
void add(int u,int v) {e[++top].to = v;e[top].ne = head[u];head[u] = top;
}
void work(int cur) {dp[cur][1] = val[cur];for(int i = head[cur]; i!=-1;i=e[i].ne) {work(e[i].to);dp[cur][0] += max(dp[e[i].to][1] , dp[e[i].to][0]);dp[cur][1] += dp[e[i].to][0];}}
int main()
{int a,b;while(cin>>n) {memset(head,-1,sizeof(head));memset(in,0,sizeof(in));top=0;for(int i = 1; i<=n; i++) {scanf("%d",&val[i]);dp[i][0]=0;dp[i][1]=0;}while(scanf("%d%d",&a,&b)) {if(a == 0 && b == 0) break;add(b,a);in[a]++;}for(int i = 1; i<=n; i++) {if(in[i] == 0) {work(i);printf("%d\n",max(dp[i][1],dp[i][0]));break;}}}return 0 ;
}

【HDU - 1520】Anniversary party (树形dp)相关推荐

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

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

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

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

  3. 宋体节点hdoj 1520 Anniversary party(树形dp)

    每日一贴,今天的内容关键字为宋体节点 标题链接 按照级等我们可以建一颗树,如图 我们可以把一个节点当作一个人,每一个节点都有一个权重.按照标题意思,如果我们取了某个节点,那么他的父节点和子节点都是不能 ...

  4. HDU 1520 Anniversary party(树形dp)

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

  5. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  6. POJ - 2342 Anniversary party(树形dp入门)

    题目链接:点击查看 题目大意:每个人都有一个快乐值,给定一个树状的从属关系,仅当上司和下属都不在的时候这个个人的快乐值才能表现出来,问怎么样才能让整体的快乐值达到最大 题目分析:做线段树做吐了,来换换 ...

  7. HDU 3534 Tree (经典树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 ...

  8. HDU 1011(星河战队 树形DP)

    题意是说在一个洞穴中有许多房间,每个房间中有一些虫子和大脑,这些房间之间用隧道相连形成一棵树,士兵们杀虫子的能力有限,也可以直接杀死虫子而不消耗士兵战斗力,但这样就无法得到房间中的大脑,士兵们不能走回 ...

  9. hdu 4044 GeoDefense (树形dp | 多叉树转二叉树)

    题目链接:hdu-4044 题意 这是一个塔防游戏,地图是一个n个编号为1-n的节点的树, 节点1是敌人的基地,其他叶子节点都是你的基地.    敌人的基地会源源不断地出来怪兽,为了防止敌人攻进你的基 ...

  10. HDU.5909.Tree Cutting(树形DP FWT/点分治)

    题目链接 \(Description\) 给定一棵树,每个点有权值,在\([0,m-1]\)之间.求异或和为\(0,1,...,m-1\)的非空连通块各有多少个. \(n\leq 1000,m\leq ...

最新文章

  1. 如何做到尽可能不使用庞大的jQuery
  2. 区块链技术指2.1 区块链技术
  3. (chap4 Http状态码) 3XX重定向
  4. aspx ajax获取后台数据,aspx界面中,怎么调用后台的方法,处理某个数据
  5. 医疗软件产品核心算法部分说明--转载截取
  6. 大规模计算时代:深度生成模型何去何从
  7. CentOS6.x 下 /etc/security/limits.conf 被改错的故障经历
  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-前言与目录(持续更新中...)
  9. Det3D - 首个通用 3D 目标检测框架
  10. ${pageContext.request.contextURI} 无效
  11. 计算机网络机械特性是指,《计算机网络与通信》第1——3章 习题及思考题
  12. 大佬是如何实现本地通讯的?
  13. HDU 1022 Train Problem I stack 基础题
  14. java 服务端 处理跨域_javaweb服务端跨域支持
  15. java程序自动重启_java程序自动重启
  16. gimp 架构_常见GUI 框架
  17. 匈牙利算法解决指派问题(java版)
  18. 【小程序合集】来一组适合你的表情包-表情包大全
  19. [转帖]*野外生活手册~
  20. oracle 11 ora 12514,客户端连接oracle11出现提示ORA-12514:错误解决方法

热门文章

  1. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第24篇]描述一个二进制m组的滑动窗口指数算法
  2. unionall mysql_5分钟了解MySQL5.7union all用法的黑科技
  3. python 创建类_python 用type()创建类
  4. linux下tar包安装sudo命令,ubuntu12.04LTS安装gv-412-Linux-x86.tar.gz方法
  5. linux下的awk程序在哪里编写,如何编写awk命令和脚本
  6. python while九九乘法表儿歌_python使用while循环实现九九乘法表
  7. ssm框架里面前端拿HTML写,ssm框架引入Vue,声明式渲染,标签的href拼接字符串
  8. java拆装_Java自动拆装包
  9. 实例解析linux内核I2C体系结构(1)
  10. linux 修改Db2主机名,修改DB2服务器的主机名