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. [CF700E][JZOJ5558]Cool Slogan (后缀自动机+线段树)

    题意翻译 给出一个长度为$n$的字符串$s[1]$,由小写字母组成.定义一个字符串序列$s[1....k]$,满足性质:$s[i]$在$s[i-1]$ $(i>=2)$中出现至少两次(位置可重叠 ...

  2. java操作AWS S3一些坑记录

    1,aws sdk jar版本不一致问题 一开始我在pom.xml中只配置了如下aws-java-sdk-s3 <!-- https://mvnrepository.com/artifact/c ...

  3. codis__使用注意事项

    codis 不支持批量的命令, codis对 redis-server 的最低版本要求是 2.8.13

  4. centos下部署NFS

        一. NFS简介   NFS---Network File System:主要功能是通过网络让不同的linux主机系统间可以彼此共享文件和目录.NFS客户端可以通过挂载的方式将NFS服务器端共 ...

  5. modalTransitionStyle各种present效果

    coverVertical(默认的) flipHorizontal crossDissolve partialCurl

  6. js数组的常用操作

    数组合并 var arr=[1,"abc","张三","122"]; var b=["今天天气不错","适合学 ...

  7. RPC与本地调用的区别

    RPC远程调用:一般可以跨平台通讯,采用http协议.因为http协议底层使用socket技术,只要你的语言支持socket技术,就可以相互进行通讯.比如:java语言开发的接口,http协议,然后让 ...

  8. 在MonoGame中SetRenderTarget会把后备缓冲区清除的解决方法

    在MonoGame中SetRenderTarget会把后备缓冲区清除的解决方法: 在构造函数中添加事件:graphics.PreparingDeviceSettings += Graphics_Pre ...

  9. @RequestMapping 和 @RequestBody的区别

    @RequestMapping要求:application/x-www-form-urlencoded 或不填 @RequestBody要求: application/json

  10. 关于:Mac下IDEA更换Maven仓库

    一.MAC安装配置maven环境变量 1.下载maven包: 下载链接: