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...... 区 ...
随机推荐
- JNI调用问题(部分机型崩溃)
1.今日测试发现在部分手机上游戏会崩溃,通过logcat日志发现是jni调用问题(我猜测) 错误日志中有如下语句: trying to work around app JNI bugs, but di ...
- C/C++ - 结构体实际申请的空间
C/C++ - 结构体实际申请的空间 如下的结构体,sizeof()大小,实际申请的空间以及理论上申请最佳空间 struct Spot { int x; int y; bool visible; in ...
- apicloud教程1 (转载)
非常感谢APICloud官方给我版主职位,每天都看到很多朋友提出很多问题,我就借此机会写了一系列的教程,帮助大家从小白到高手之路.系列名称:<APICloud之小白图解教程系列>,会不定时 ...
- 软件设计模式详解:OCP原则
看到两篇关于OCP的文章, 纳之. 原文: http://www.cnblogs.com/muzongyan/archive/2010/08/05/1793454.html http://blog. ...
- Java 多态,重载,重写
1.多态(polymorphism): 多态是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象, ...
- php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式浅谈
做 php 开发的应该都知道 php 运行模式概念吧,本文将要和大家分享的是关于php目前比较常见的五大运行模式:包括cgi .fast-cgi.cli.isapi.apache模块的DLL ,下面作 ...
- Angularjs循环二维数组
<div ng-app> <div ng-controller="test"> <div ng-repeat="links in slide ...
- jQuery插件---轻量级的弹出窗口wBox
Box Demo wBox——轻量级的弹出窗口jQuery插件,基于jQuery1.4.2开发,主要实现弹出框的效果,并且加入了很多有趣的功能,比如可img灯箱效果,callback函数,显示隐藏层, ...
- Mysql命令-以NULL做where条件过滤时应该写 IS NULL;
以NULL做where条件过滤时应该写 IS NULL;SELECT * FROM pet WHERE death IS NULL; SELECT * FROM pet WHERE death IS ...
- the.book.of.gimp.pdf文字不显示
逆天了,不是中文也不显示. https://bugs.freedesktop.org/show_bug.cgi?id=70529 说要升级libfreetype,可是已经是wheezy最新了,其他不稳 ...