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. php生成N个不重复的随机数实例

    思路: 将随机数存入数组,再在数组中去除重复的值,即可生成一定数量的不重复随机数. /* * array unique_rand( int $min, int $max, int $num ) * 生 ...

  2. linux密码修改实验

    1.在单用户模式下进行引导 在不同的运行级别中,一个重要的运行级别就是单用户模式(运行级别1),该模式中,只有一个系统管理员使用特定的机器,而且尽可能少地运行系统服务,其中包含登录.单用户模式对少数管 ...

  3. Python自动发送HTML测试报告

    在我们做自动化测试的时候,执行完所有的测试用例,我们是希望马上得到结果的,那么我们不可能一直盯着去看,这个时候就需要引入邮件功能 1.首先我们使用一个python的第三方发邮件的库 yagmail g ...

  4. XMPP Authentication

      From: http://www.ietf.org/rfc/rfc2831.txt 2 Authentication The following sections describe how to ...

  5. Smarty带来的神秘的数字1

    问题的引发:在htmly页面通过smarty模板引擎开启session_start()后,突发发现页面无故多了一个  神秘的数字 1 问题界面: 代码: 测试:在session_start()行末加2 ...

  6. Rstudio常用且不熟快捷键 “原版+中文” 整理

  7. python创建目录并更改权限的代码

    如下代码段是关于python创建目录并更改权限的代码. import os os.mkdir("foo", 0666)

  8. js 删除数组的某一项或者几项的方法

    1.arr.splice() splice(index,len,[item])    注释:该方法会改变原始数组. splice有3个参数,它也可以用来替换/删除/添加数组内某一个或者几个值 inde ...

  9. 实现接口必须要加注解@Override吗

    不一定的,但是我们的编译器在查询我们重写的方法,方法名,参数,返回类型的时候,是能够根据注解来帮助我们判断方法重写的正确与否 所以我们有必要在编写过程中加上@Override,虽然我们的eclipse ...

  10. 换个角度看Salesforce之基础配置学习笔记(二)

    1. 登录后无法使用Developer Console? 先找到当前登录用户的Profie,然后勾选Profile中的View All Data(Modify All Data)即可: 2. Pers ...