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. [agc004d]Teleporter 暴力

    Description 维尼管辖的领土很大,我们可以抽象为n个城市,其中1号点为首都.这n个城市之有n条单向电缆,一条信息经过一条电缆进行传输所需时间会+1s,然而维尼并不能忍受时间白白被续,他要求从 ...

  2. docker导入导出镜像

    docker容器导入导出有两种方法: 一种是使用save和load命令 使用例子如下: docker save ubuntu:load>/root/ubuntu.tar docker load& ...

  3. struts中如何查看配置文件中是否存在某个返回值

    ActionConfig config = ActionContext.getContext() .getActionInvocation().getProxy().getConfig(); Resu ...

  4. c调用 lua 栈操作

    转自https://www.cnblogs.com/ringofthec/archive/2010/10/22/lua.html 打算记录一些lua_api, 可能会觉得lua文档中已经说的很清楚了, ...

  5. Bowen

    Advertise Window大小 注册表键值位于:regedit->HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Pa ...

  6. zero-copy总结

    基本概念 零拷贝,通常在java NIO编程中会使用,比如netty网络工具包. 其真实意思是: 网卡或者其他外设进行io操作时不经过CPU, 而是直接和主memory交互,不经过CPU寄存器,这样可 ...

  7. UML-4-初始不是需求阶段

    1.初始阶段到底做什么? 确定项目外包还是自研.成本多少. 系统边界 关键的10%的用例.非功能性需求分析,业务案例创建 开发环境准备 而需求分析是在细化阶段进行的. 2.初始阶段(或细化阶段早期)应 ...

  8. DataGridView绑定list的注意事项

    1.DataGridView数据绑定对比(DataTable与泛型List):       当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时, ...

  9. python pandas使用一些协程

    import pandas as pd def coroutine(func): """装饰器:向前执行到第一个`yield`表达式,预激`func`"&quo ...

  10. Ubuntu 18.04上搭建FTP服务器

    1.准备工作需要安装并运行的Ubuntu Server 18.04系统.当然还需要一个具有sudo权限的账号. 2.安装VSFTPVSFTP程序位于标准存储库中,因此可以使用单个命令删除安装.打开终端 ...