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. Android再学习-20141022-Activity的生命周期

    20141022-Android再学习 如何在一个应用程序当中定义多个Activity 定义一个类,继承Activity 在该类当中,复写Activity当中的onCreate方法.onCreate( ...

  2. 百度云观测优化建议解决方案:未设置max-age或expires

    网页的缓存是由 HTTP 消息头中的 “Cache-control” 来控制的,常见的取值有 private.no-cache.max-age.must-revalidate 等,默认为private ...

  3. 怎样使用pyinstaller打包

    安装好pyinstaller后 cd 到pyinstaller.py目录,在命令行输入:python pyinstaller.py 参数 主文件所在目录 如:python pyinstaller.py ...

  4. C/C++ char和int的区别

    字符字面值一般是用一对单引号来表示.char类型一般就是用字符字面值来初始化.赋值.由于char类型的是单字节长度,当给char类型的变量用字符字面值赋值时,当单引号里面的内容超过一个字节时,系统会自 ...

  5. RichTextBox控件日常使用集合

    1.RichTextBox控件自动滚动到底部 richTextBox1.ScrollToCaret(); //将控件的内容滚动到当前光标位置

  6. Struts2学习笔记--Struts2的体系结构

    1. Struts2体系结构 Struts是以前端控制器框架为主体的框架,用户的请求会通过控制器选择不同的Action类来执行具体的操作,在Action类中所有的Servlet对象(request.r ...

  7. 在VS2010下开发C语言程序

    妈蛋,我发现VS下开发C语言,并不是把文件名改成.C的就行了.VS2010不支持C99.  也就是函数局部变量声明必须放在函数的开头.难怪,我从Linux下移植过来的.c工程怎么是编译错误呢,发现是变 ...

  8. js深入研究之神奇的匿名函数类生成方式

    <script type="text/javascript"> var Book = (function() { // 私有静态属性 ; // 私有静态方法 funct ...

  9. MyBatis映射文件的resultMap如何做表关联

    MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射 ...

  10. 关于bootstrap--表格(tr的各种样式)

    只需要<tr class="active">就可以用active样式. 特别提示:除了”.active”之外,其他四个类名和”.table-hover”配合使用时,Bo ...