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

Anniversary party

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

题意:给出一个员工关系图(树),每个员工有一个上司和一个快乐度,现在要参加一个派对,不能让员工和直接上司一起到场,求现场能达到的最大快乐度是多少。

思路:比较水的题目,一个 t[i] 记录第 i 个员工出场的时候以 i 为根的树的总值,f[i] 记录第 i 个员工不出场的时候以 i 为根的树的总值。

   f[i]的时候他的儿子可选可不选(一开始只考虑选的情况错了几次,有时候不选更优),t[i]的时候他的儿子一定不可选。

   还有一个坑点是有多个case的。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 6010
struct node
{
int nxt, v;
}edge[N];
int head[N], tot;
int w[N], deg[N];
int f[N], t[N];
int s[N]; void add(int u, int v)
{
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
} void dfs(int s)
{
for(int i = head[s]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
dfs(v);
f[s] += max(t[v], f[v]); //可以选或者不选
t[s] += f[v];
}
} int main()
{
int n;
while(~scanf("%d", &n)) {
memset(f, , sizeof(f));
memset(t, , sizeof(t));
memset(deg, , sizeof(deg));
memset(head, -, sizeof(head));
tot = ;
for(int i = ; i <= n; i++) {
scanf("%d", &w[i]);
t[i] += w[i];
}
int u, v;
while() {
scanf("%d%d", &u, &v);
if(u + v == ) break;
add(v, u);
deg[u]++;
}
int cnt = ;
for(int i = ; i <= n; i++) {
if(deg[i] == ) {
s[cnt++] = i;
}
}
int ans = ;
for(int i = ; i < cnt; i++) {
dfs(s[i]);
ans += max(f[s[i]], t[s[i]]); //万一有很多个根
}
printf("%d\n", ans);
}
return ;
}

HDU 1520:Anniversary party(树形DP)的更多相关文章

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

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

  2. HDU 1520 Anniversary party [树形DP]

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

  3. hdu oj 1520 Anniversary party(树形dp入门)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  5. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  6. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

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

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

  8. HDU 1520 Anniversary party(DFS或树形DP)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

  9. TTTTTTTTTTT hdu 1520 Anniversary party 生日party 树形dp第一题

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. hdu Anniversary party 树形DP,点带有值。求MAX

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. WPF 程序检测 windows 关机

    1.使用SessionEnding事件 参考MSDN. 2.在需要处理 关机事件中可以注册SessionEnding事件 App.Current.SessionEnding += Current_Se ...

  2. MongoDB操作

    创建.删除数据库 格式 use DATABASE_NAME 如果不存在,则创建,否则直接切换到该数据库 显示当前所在的数据库 db 显示所有数据库 show dbs 删除数据库 db.dropData ...

  3. Vue.2.0.5-组件

    什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.在 ...

  4. 让show parameter显示隐含参数(11G)

    1.创建视图show_hidden_v$parameter create or replace view show_hidden_v$parameter (INST_ID, NUM , NAME , ...

  5. redhat linux 安装mysql5.6.27

    1.yum安装mysql(root身份) yum install mysql-server mysql-devel mysql -y 如没有配置yum,请参见博客:http://www.cnblogs ...

  6. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

  7. UVa 10088 - Trees on My Island (pick定理)

    样例: 输入:123 16 39 28 49 69 98 96 55 84 43 51 3121000 10002000 10004000 20006000 10008000 30008000 800 ...

  8. 最新RubyMine2016.2开发Ruby ON Rails(ROR)程序的流程

    1.RubyMine新建ROR工程 File->New Project     选择Rails下的"New Application"     点击OK 后生成ROR项目   ...

  9. Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别

    Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...

  10. POJ 3356 AGTC(DP-最小编辑距离)

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...