Anniversary party

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

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
 
 
典型的树形dp,很简单的。
题意:大致:校庆,邀请老师们,老师们之间有严格的层次关系,是一颗树。
      每个老师有自己的一个值rating。
      不能同时邀请 “下属” he 他/她 的直接上司。
      求满足这样的 最大的rating 和。
思路:  寻找根节点通过标记 L , 如果没有被标记过,就代表没有父亲,那就是root了。
          设dp[ k ] [ 0 ] 为不包含 k 点时,最大的和。
      设dp[ k ] [ 1 ] 为   包含 k 点时,最大的和。
    
          对于叶子节点:   dp[ k ] [ 0 ]= 0;    dp[ k ] [ 1 ]= rating[ k ];
          对于非叶子节点:
                                dp[ k ] [ 0 ]= sum{      Max(dp[ j ] [ 0 ], dp[ j ] [ 1 ])       }; 
                                                          //dp[ j ] [ 0 ]  , dp[ j ] [ 1 ] 是K的 儿子节点。
                                dp[ k ] [ 1 ]= sum{  dp [ j ] [ 0 ] } + rating[ k ];
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std; struct node
{
int next[];
int num;
}f[];
int a[];
bool use[];
int dp[][]; int Max(int x,int y)
{
return x>y? x:y;
} void dfs(int k)
{
int i,cur,j;
if(f[k].num==)
{
dp[k][]=;
dp[k][]=a[k];
return;
}
for(i=;i<=f[k].num;i++)
{
j=f[k].next[i];
dfs(j);
cur=Max(dp[j][],dp[j][]);
dp[k][]+=cur; dp[k][]+=dp[j][];
}
dp[k][]+=a[k];
} int main()
{
int n,i,root,x,y;
while(scanf("%d",&n)>)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]); for(i=;i<=;i++)
f[i].num=;
memset(use,false,sizeof(use));
memset(dp,,sizeof(dp));
scanf("%d%d",&x,&y); while()
{
if(x==&&y==)break;
f[y].num++;
f[y].next[f[y].num]=x;
use[x]=true;
scanf("%d%d",&x,&y);
} for(i=;i<=n;i++)
if(use[i]==false)
{
root=i;
break;
}
dfs(root);
printf("%d\n",Max(dp[root][],dp[root][]));
}
return ;
}
                   
 

hdu Anniversary party 树形DP,点带有值。求MAX的更多相关文章

  1. poj 2324 Anniversary party(树形DP)

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

  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. HDU 2196 Computer 树形DP 经典题

    给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...

  5. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  6. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  7. HDU 2196.Computer 树形dp 树的直径

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  9. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

随机推荐

  1. Mysql创建、删除用户[转]

    MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束): 1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 ...

  2. JMeter—监听器

    用来显示JMeter取样器的测试结果,能够以树.表.图形形式显示,也可以以文件方式保存. 一.设置默认配置 初始化配置文件设置: 监听器默认保存哪些数据域,可以在jmeter.properties(或 ...

  3. C# - 图片操作和Base64处理

    旋转 (1)按角度旋转 /// <summary> /// 根据角度旋转图标 /// </summary> /// <param name="img" ...

  4. 编写高质量代码:Web前端开发修炼之道(二)

    第四章:高质量的css 1)怪异模式和标准模式 在标准模式中,浏览器根据规范表现页面:而怪异模式通常模拟老式浏览器的行为以防止老站点无法工作. 他们两者之间的差异比较典型的表现在IE对盒模型的解析:在 ...

  5. ThreadFactory类的使用

    之前创建线程的话,基本上是使用new Thread(),或者是将任务提交到线程池执行.今天看了一下洁城浩的<图解java多线程设计模式>突然看到还可以使用ThreadFactory来创建一 ...

  6. 使用Django-environ管理多个配置

    使用Django-environ管理多个配置 https://django-environ.readthedocs.io/en/latest/

  7. jQuery 学习笔记(jQuery: The Return Flight)

    第一课. ajax:$.ajax(url[, settings]) 练习代码: $(document).ready(function() { $("#tour").on(" ...

  8. vue过渡效果

    vue过渡效果. <transition name='slide-fade'> <div class="top" @click='gotoTop' v-if='s ...

  9. ios UITableView 异步加载图片并防止错位

    UITableView 重用 UITableViewCell 并异步加载图片时会出现图片错乱的情况 对错位原因不明白的同学请参考我的另外一篇随笔:http://www.cnblogs.com/lesl ...

  10. ZOJ 2856 Happy Life

    Problem Description Do you know Utopia? It's a perfect world in which everyone leads a happy life. A ...