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. 防止IE不支持console.log报错

    function log(msg){ if (window["console"]){//判断是否是IE console.log(msg); } }

  2. iPhone尺寸规范

    转载于:http://www.uigreat.com/page/guifan

  3. tooltip

    /* 背景色 ; 字体颜色 ; 云,显示在上面 */ .tooltip-inner{ background-color: #FF0000; ForeColor:#0f0; IsBalloon:true ...

  4. sql语句感想

    select出来内容可以当成表拿来用,,比如取别名什么的. union是纵向的,追加记录(行) join on是横向的,追加列

  5. CheckBox的用法

    if (window.pageConfig["IsCommend"] == "True") {      $("#IsCommend").v ...

  6. 、JAVA-异常

    异常 1.种类(error 系统异常,无法处理)(exception 程序异常,可以处理) 1.算数异常 2.空指针异常 原因:对象没有实例化就调用他的实例方法,会造成空指针异常 2.常见异常 1.R ...

  7. IOS Certificates 制作流程 (Adobe FlashBuilder)

    看到一个Adobe 官方的PDF 说的非常明白,比自己一步一步总结的要好多了 怎么生成 以及为何生成都有说明 http://help.adobe.com/en_US/ppcompdoc/Adobe_P ...

  8. 前端新手分析 AJAX执行顺序,数据走向

    我是一名前端的newer 在刚学习AJAX和eJS的时候,对于顺序上面有很大迷惑,现在稍微清楚了一点, 理解不对的地方,还请各位大牛帮助给我指导一下. 总的 服务器和客户端的顺序   一. 除了必要的 ...

  9. [转]gitHub客户端Desktop的安装使用总结 ---基础篇

    gitHub客户端Desktop的安装使用总结 ---基础篇 发表于2015/12/11 11:41:57  8399人阅读 分类: Android之应用实战 这段时间想把我写的东西上传到github ...

  10. 夺命雷公狗—angularjs—24—extend继承对象

    我们的angularjs中也是给我们留下了方法来做继承的,那么他就是传授中的extend... 不过要如下所示,第二个参数是继承到第一个对象里面的... <!DOCTYPE html> & ...