A - 树形dp

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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 N – 1 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 
题目大意:给你一 棵关系树,让你从中选择若干个人,这些人之间不能有直接的上下级关系,要求最后的到的权值最大
解析见代码:
/*
hdu1520
简单树状DP
解题包括以下几步
1.关系树的建立(用一个结构体来储存某个节点所包含的信息。)
2.确定状态转移方程 f[i][0]表示不选该节点,f[i][1]表示选择该节点,子树能够得到的最大权值
j为i的子节点,f[i][1]=1+f[j][0],f[i][0]=max(f[j][0],f[j][1])
最后答案就是max(f[root][1],f[root][0])
3.编程实现,记忆化搜索,相当于树的后序遍历(从子到根)
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn = + ;
int fa[maxn];
int f[maxn][];
int indegree[maxn];
bool vis[maxn];
int v[maxn];
int n;
void dfs(int x)
{
vis[x]=true;
for(int i=;i<=n;i++)
{
if(!vis[i]&&fa[i]==x)
{
dfs(i);
f[x][]+=max(f[i][],f[i][]);
f[x][]+=f[i][];
}
}
}
int main()
{
int a,b;
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
scanf("%d",&v[i]);
memset(indegree,,sizeof(indegree));
memset(outdegree,,sizeof(outdegree));
while(scanf("%d%d",&a,&b))
{
if(a==&&b==) break;
fa[a]=b;
indegree[a]++;
}
memset(f,,sizeof(f));
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)//边界初始化
{
f[i][]=v[i];
}
// cout<<indegree[5]<<endl;
for(int i=;i<=n;i++)
{
if(indegree[i]==)
{
dfs(i);
printf("%d\n",max(f[i][],f[i][]));
break;
}
}
}
return ;
}

hdu1520 第一道树形DP,激动哇咔咔!的更多相关文章

  1. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  2. hdu 2476(第一道区间dp)

    题意:就是给定两个字符串,第一个是初始串,第二个是目标串,问你把初始串变到目标串最少需要多少串! 分析:此题分两步,第一步是假设开始的初始串是空串,然后就进行区间dp,dp[i][j]代表把区间[i, ...

  3. hdu1520 Anniversary party (树形dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...

  4. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  5. HDU1520 Anniversary party —— 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  6. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  7. hdu 1561 The more, The Better(树形dp,基础)

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  9. hud1520Anniversary party(树形DP)

    链接 第一道树形DP 根据左儿子 右兄弟 将多叉树转化成二叉树 结构体里保存取这个节点和不取这个节点的最大值 #include <iostream> #include<cstdio& ...

随机推荐

  1. MFC 创建选项卡

    1.创建三个选项卡Dialog窗体,ID分别改为porpTest1.porpTest2.porpTest3 2.创建三个选项卡类,类名分别为CPropTest1.CPropTest2.CPropTes ...

  2. [置顶] chinayaosir近10年来所阅读的世界著名IT书籍-图文并茂

    1.人生观(包括做人原则,心理学,投资,销售) 一个人从来到世上,很多东西都是空白, 阅读一些正能量的书籍,把里面的理论用于生活,不断的应用它, 这些观念就会如同软件一样,不断的升级你的大脑, 合理的 ...

  3. curl抓取信息

    <?php $hotel = new curl(false,0); $str = $hotel -> post("http://www.todayinns.com/login.p ...

  4. Okhttp3日志采集功能

    原文地址以示尊重:http://www.jianshu.com/p/d836271b1ae4 日志采集是一个APP必备的功能,可以方便开发人员快速定位问题,解决问题,那么我们在使用okhttp的时候应 ...

  5. 郁闷~win7无法进行局域网访问解决

    win7无法进行局域网访问解决 公司里经常会使用网络共享的文件服务器,但是用win7的那伙计,在输入帐号和密码后却提示密码错误,试验了多次都是如此. 经过网上搜索文章及自己的研究最后发现这是由于win ...

  6. (转)Oracle Data Guard学习

    一.Data Guard提供如下三种数据保护模式: 1)最高保护模式(Maximum Protection) 这里的”最高保护“是指最大限度的保护数据不丢失,也就是至少有一个standby和prima ...

  7. android 中View, Window, Activity, WindowManager,ViewRoot几者之间的关系

    (1)View:最基本的UI组件,表示屏幕上的一个矩形区域. (2)Window: 表示一个窗口,不一定有屏幕那么大,可以很大也可以很小:                         它包含一个V ...

  8. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  9. smarty 内置函数if 等判断

    {if},{elseif},{else} Smarty的{if}条件判断和PHP的if 非常相似,只是增加了一些特性. 每个{if}必须有一个配对的{/if}. 也可以使用{else} 和 {else ...

  10. java I/O之装饰者模式

    装饰者: Decorator模式(别名Wrapper):动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案. 装饰者模式意图: 动态的给一个对象添加额外的职责.Decorato ...