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语言写猜拳游戏中遇到的函数循环小问题
各位可能在初学C语言的时候都有写过猜拳游戏.但在写猜拳的函数时,避免不了会使用循环. 当函数被套在一个循环中的时候,你的计分变量可能就会被重置为函数体里的初始值.那么怎么解决这个问题? 其实很简单,你 ...
- Histogram Equalization
转载请注明出处. Histogram Equalization 也就是直方图均衡化, 是一种常用的通过直方图处理来增强图像的方法. 对于一副灰度图像,其像素范围一般在0~255之间,我们记nk(0&l ...
- css 画竖着线条
<p>table控制:<!-- height为横线的粗度,width为长度 --><table width=700> <tr> <t ...
- iOS开发中文件的上传和下载功能的基本实现-备用
感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...
- cf C. Secrets
http://codeforces.com/contest/334/problem/C #include <cstdio> #include <iostream> #inclu ...
- windows下查看端口占用情况
最近在用ICE做分布式应用 https://doc.zeroc.com/pages/viewpage.action?pageId=5048454 写了一个client 和server.server监听 ...
- windows设备驱动安装指南
高观点下的设备驱动安装(overview) 一.windows是怎样安装设备的? 第一步:新设备的识别 在给一个新设备安装驱动之前,总线或集线器(hub)驱动会为连接到PC上的设备分配一个硬件ID(h ...
- PHP页面静态化(转)
在很多地方都看到有PHP整站静态化的东东,怪唬人的..其实,你会静态化一个页面,那么别说整站了,想静态化多少都可以.所以关键是,首先要知道怎么静态化一个页面,了解静态化的原理是关键.. 这里就说下我个 ...
- easyui获取日期datebox中的值
<input type="text" class="easyui-datebox" id="CTIME" style="wi ...
- C# 约瑟夫环算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...