hdu1520 第一道树形DP,激动哇咔咔!
Description
Input
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
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,激动哇咔咔!的更多相关文章
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- hdu 2476(第一道区间dp)
题意:就是给定两个字符串,第一个是初始串,第二个是目标串,问你把初始串变到目标串最少需要多少串! 分析:此题分两步,第一步是假设开始的初始串是空串,然后就进行区间dp,dp[i][j]代表把区间[i, ...
- hdu1520 Anniversary party (树形dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...
- HDU1520 Anniversary party 树形DP基础
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...
- HDU1520 Anniversary party —— 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- HDU 1520 树形dp裸题
1.HDU 1520 Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...
- 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 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- hud1520Anniversary party(树形DP)
链接 第一道树形DP 根据左儿子 右兄弟 将多叉树转化成二叉树 结构体里保存取这个节点和不取这个节点的最大值 #include <iostream> #include<cstdio& ...
随机推荐
- C#Mysql数据库爆破源码
声明: 代码仅供学习参考使用!开启了一个子线程,进行爆破! 速度不是很快,代码不是很规范,希望大牛不要喷我! c#控制台程序,需要引用MySql.Data.dll 默认用户名: root密码字典: p ...
- mysql与java数据类型对应关系
- PHP的curl常用的5种写法
// 1,抓取无访问控制文件 $ch= curl_init(); curl_setopt($ch, CURLOPT_URL,"http://localhost/mytest/phpinfo. ...
- Apache配置rewrite
最近将代码做了迁移,更换了web服务器,从原来的Nginx,换成使用Apache,多少有些区别.这里整理一下在apache下实现rewrite功能. 第一部分:修改apache配置文件支持rewrit ...
- 比如在vi中按ctrl+z
比如在vi中按ctrl+z有个命令可以恢复会话,我忘了,大家谁记得? 分享到: 对我有用[0] 丢个板砖[0] 引用 | 举报| 编辑 删除 管理 回复次数:8 hellwolf hellwo ...
- 用js 做大图轮播方法(一)
//html部分 <div id="wrap"> <div id="slider"> <a target="_blank ...
- matlab 对图像操作的函数概览
转自博客:http://blog.163.com/fei_lai_feng/blog/static/9289962200991713415422/ 一. 读写图像文件 1. imread imread ...
- 怪胎:Android开发ImageView图片无法显示
今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片. 具体过程是:先调用本地照相机程序摄像,然后将拍摄的图片加载在ImageView中显示. publ ...
- vs2010 suite integration toolkit execution
原因是UltraDeamen的问题,重新换个WinMount来解压ISO文件.完美安装运行
- NOI2015
D1T1 并查集. #include<cstdio> #include<cstdlib> #include<iostream> #include<fstrea ...