HDU 4169 UVALive 5741 Wealthy Family
树形背包。DP递推的思路很简单....
但是由于节点有15万个,先不论空间复杂度,这样开dp数组 dp[150000+10][300+10],如果初始化是memset(dp,-1,sizeof dp),则必然超时。
所以需要一个状态数剪枝。。。即记录这个节点最多组合的数量。
UVALive是不限制内存的,所以dp[150000+10][300+10] 能够AC,HDU 4169 限制了内存大小,需要优化空间复杂度。
内存优化之后的代码,HDU上C++能AC,G++依旧MLE。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
struct Node
{
int val;
int fa;
queue<int *>Q;
vector<int>f;
}node[maxn];
int n,k,root;
int cnt[maxn];
int ans; void init()
{
memset(cnt,,sizeof cnt);
for(int i=;i<=n;i++)
{
while(!node[i].Q.empty()) node[i].Q.pop();
node[i].f.clear();
}
} void read()
{
for(int i=;i<=n;i++)
{
scanf("%d%d",&node[i].fa,&node[i].val);
if(!node[i].fa) root=i;
else node[node[i].fa].f.push_back(i);
}
} void dfs(int now)
{
if(!node[now].f.size())
{
cnt[now]=;
int *p=new int[cnt[now]+];
p[]=; p[]=node[now].val;
node[node[now].fa].Q.push(p);
delete[] p;
return;
} for(int i=;i<node[now].f.size();i++)
{
int id=node[now].f[i];
cnt[now]=cnt[now]+cnt[id];
} cnt[now]=min(k,cnt[now]); int *p=new int[cnt[now]+]; p[]=;
for(int i=;i<=cnt[now];i++) p[i]=-; int id=;
while(!node[now].Q.empty())
{
int *head=node[now].Q.front();
node[now].Q.pop(); for(int j=cnt[now];j>=;j--)
for(int s=;s<=j&&s<=cnt[node[now].f[id]];s++)
if(head[s]!=-&&p[j-s]!=-)
p[j]=max(p[j],head[s]+p[j-s]);
id++;
}
p[]=max(p[],node[now].val); node[node[now].fa].Q.push(p); if(now==)
{
if(cnt[]<k||p[k]==-) ans=-;
else ans=p[k];
} delete[] p;
return;
} void work()
{
dfs(root);
if(ans==-) printf("impossible\n");
else printf("%d\n",ans);
} int main()
{
while(~scanf("%d%d",&n,&k))
{
init();
read();
work();
}
return ;
}
二维DP写法。HDU 上MLE的。UvaLive能过的。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int n, k;
int root;
const int maxn = + ;
struct Edge
{
int now;
int next;
}e[maxn];
int head[maxn];
int cnt[maxn];
int val[maxn];
int dp[maxn][ + ];
int q; void init()
{
q=;
for(int i=;i<=n;i++) head[i]=-;
} void read()
{
for (int i = ; i <= n; i++)
{
int fa;
scanf("%d%d", &fa, &val[i]);
if (!fa) root = i;
else
{
e[q].now=i, e[q].next=head[fa];
head[fa]=q, q=q+;
}
}
} void dfs(int now)
{
cnt[now]=;
if (head[now]==-)
{
cnt[now]=;
dp[now][] = val[now];
return;
} for (int i = head[now]; i!=-; i=e[i].next)
{
int id = e[i].now;
dfs(id);
cnt[now]=cnt[now]+cnt[id];
} cnt[now]=min(cnt[now],k); for(int i=;i<=cnt[now];i++) dp[now][i]=-;
dp[now][]=; for (int i = head[now]; i!=-; i=e[i].next)
{
int id = e[i].now;
for(int j=cnt[now];j>=;j--)
for(int s=;s<=j&&s<=cnt[id];s++)
if(dp[id][s]!=-&&dp[now][j-s]!=-)
dp[now][j]=max(dp[now][j],dp[id][s]+dp[now][j-s]);
}
dp[now][]=max(val[now],dp[now][]);
} void work()
{
dfs(root);
if (cnt[root]<k||dp[root][k] == -) printf("impossible\n");
else printf("%d\n", dp[root][k]);
} int main()
{
while (~scanf("%d%d", &n, &k))
{
init();
read();
work();
}
return ;
}
HDU 4169 UVALive 5741 Wealthy Family的更多相关文章
- HDU 4169 Wealthy Family(树形DP)
Problem Description While studying the history of royal families, you want to know how wealthy each ...
- HDU 4169 树形DP
Wealthy Family Problem Description While studying the history of royal families, you want to know ho ...
- hdu 4169 二分匹配最大独立集 ***
题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...
- HDU 5741 Helter Skelter(构造法)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5741 [题目大意] 一个01相间的串,以0开头,给出的序列每个数字表示连续的0的个数或者1的个数, ...
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...
- POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)
POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- UVALive - 4223(hdu 2926)
---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS ...
- HDU 5741 Helter Skelter
离线处理+扫描线.题意很容易转化:若干个矩形形成并集,询问一些点是否在并集中? 官方题解不是这样做的....那种做法效率更高,暂时还不会.我这样是4500ms G++过的,C++TLE...... 区 ...
随机推荐
- java应用测试报告生成(一): sonarqube配合Jenkins生成测试报告及覆盖率
环境准备: 1.Jenkins集成环境(安装 sonarqube插件) 2.安装sonarqube服务(下载sonarqube安装包并解压,目录到"sonarqube-5.4/bin/lin ...
- OpenGL ES着色器语言之静态使用(static use)和预处理
OpenGL ES着色器语言之静态使用(static use) 在OpenGL ES中有一个术语叫静态使用(static use),什么叫静态使用呢? 在写代码中,对于一个变量可能具有以下三种情况: ...
- SSH整合中为获取表单对象Action类实现的接口及拦截器配置
保存员工或者用户信息时,以员工为例,是通过表单收集信息的,需要把这些信息赋给一个对象,然后保存到数据库中.对应的Action类须实现Preparable接口及ModelDriven接口,且在Actio ...
- 快学Scala-第二章 控制结构和函数
知识点: 1.条件表达式 if(x>0) 1 else 0 scala每个表达式都有一个类型,如果一致则为它们的类型,如果为混合类型表达式,则为它们的公共超类型Any. if(x>0) 1 ...
- UVALive 2521 Game Prediction 题解
这个我上来把题目理解错了,我以为所有人的牌都是一样的,感觉这个题太麻烦了吧,而且题目样例过不去啊……后来发现理解错了,给出的数据是他一个人的数据,就是让我们求他一定能赢的轮数,所有的牌是固定的(1 - ...
- 读《Ext.JS.4.First.Look》随笔
Ext JS 4是最大的改革已经取得了Ext框架.这些变化包括一个新类系统,引入一个新的平台,许多API变化和改进,和新组件,如新图表和新画组件.Ext JS 4是更快,更稳定,易于使用.(注意:Ex ...
- Git学习 -- 分支管理
创建新分支,并切换到该分支 git checkout -b dev 这一句相当于以下两句: git branch dev 创建 git checkout dev 切换 查看当前分支 git branc ...
- Oracle Sql优化之Merge 改写优化Update
1.待改写语句如下 update table1 f )),) ,),)), f.jine2 )),) from table2 e where e.kjqj=f.kjqj=e.gs=f.gs and e ...
- 微信小程序 问题收集
1. Q: 想知道微信小程序这些证书的具体要求在哪儿能查到? 就比如说,他要求有卫生和计划生育委员会批文 我想知道是需要省级还是市级的 但是官网查不到 A:市级省级的我也不是特清楚 [/ak] 应该是 ...
- 【转载】最全的面试题目整理(HTML+CSS部分)
转载自 知乎 @西点王子 https://www.zhihu.com/people/F211/answers 1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? (Q1) 浏览器: ...