http://blog.csdn.net/dellaserss/article/details/8799730

这题其实和上一题思路是一样的,一个0节点作为根节点,通过剩余量来遍历子树。

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 205
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m; struct node
{
int y,next;
}tree[]; int head[MAXN],dp[MAXN][MAXN],ptr=; int sum[MAXN],vl[MAXN],vis[MAXN]; void add(int x,int y)
{
tree[ptr].y = y;
tree[ptr].next = head[x];
head[x] = ptr++;
} void dfs(int root)
{
if(vis[root]) return;
int i,j,k;
vis[root] = sum[root] = ;
int tot = ; for(i=head[root]; i!=-; i=tree[i].next)
{
int p = tree[i].y; if(!vis[p])
{
dfs(p);
sum[root]+=sum[p];
tot++;
//pf("i%d p%d tot%d sum%d\n",root,p,tot,sum[root]);
}
} dp[root][] = vl[root]; for(i=head[root]; i!=-; i=tree[i].next)
{
int p = tree[i].y; for(j = sum[root];j>;j--)
{
for(k = ;k<j;k++)
{
if(dp[root][k]!=- && dp[p][j-k]!=-)
{
dp[root][j] = max(dp[root][j],dp[root][k]+dp[p][j-k]);
//pf("i%d j%d k%d p%d dp%d\n",root,j,k,p,dp[root][j]);
}
}
}
}
} int main()
{
int i,j,k,a,b;
while(~sf("%d%d",&n,&m) && m+n>)
{
mem(head,-);
ptr = ; for(i=;i<=n;i++)
{
sf("%d%d",&a,&b);
add(i,a);
add(a,i);
vl[i] = b;
}
mem(dp,-);
mem(vis,);
dfs();
pf("%d\n",dp[][m+]);
}
return ;
}

但我发现这道题因为要统计子节点数量,其实用邻接表更方便一些:(但速度会慢很多)

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 205
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m; struct node
{
int y,next;
}tree[]; int head[MAXN],dp[MAXN][MAXN],ptr=; int sum[MAXN],vl[MAXN],vis[MAXN]; vector<int> son[]; void dfs(int root)
{
int i,j,k; for(i=;i<son[root].size();i++)
{
int p = son[root][i]; if(son[p].size()>) dfs(p); for(j=m;j>;j--)
{
for(k=;k<j;k++)
{
dp[root][j] = max(dp[root][j],dp[root][k] + dp[p][j-k]);
//pf("i%d j%d k%d p%d dp%d\n",root,j,k,p,dp[root][j]);
}
}
}
} int main()
{
int i,j,k,a,b;
while(~sf("%d%d",&n,&m) && m+n>)
{
mem(dp,);
m++; for(i=;i<=n;i++) son[i].clear(); for(i=;i<=n;i++)
{
sf("%d%d",&a,&b);
son[a].pb(i);
for(j=;j<=m;j++) dp[i][j] = b;
}
dfs();
pf("%d\n",dp[][m]);
}
return ;
}

hdu 1561 树形背包 选k个最大价值的更多相关文章

  1. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  2. HDU 1561 (树形DP+背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1561 题目大意:从树根开始取点.最多取m个点,问最大价值. 解题思路: cost=1的树形背包. 有 ...

  3. HDU 1561 树形DP(入门)

    题目链接:  HDU 1561 The more, The Better #include <iostream> #include <cstdio> #include < ...

  4. hdu 1561 树形dp+分组背包

    题意:就是给定n个点,每个地点有value[i]的宝物,而且有的宝物必须是另一个宝物取了才能取,问取m个点可以获得的最多宝物价值. 一个子节点就可以返回m个状态,每个状态表示容量为j(j<=m) ...

  5. hdu 1561 树形DP n个选m个价值最大

    http://acm.hust.edu.cn/vjudge/problem/18068 #include <iostream> #include <string> #inclu ...

  6. hdu 1561(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 思路:dp[u][i]表示以u为根的树选了i个子节点. #include<iostream ...

  7. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. HDU 1561 树形DP背包问题

    这是自己第一道背包上树形结构问题,不是很理解这个概念的可以先看看背包九讲 自己第一次做,看了一下别人的思路,结合着对简单背包问题的求解方式自己一次AC了还是有点小激动的 题目大意是: 攻克m个城市,每 ...

  9. HDU 2639(01背包第K大)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 http://blog.csdn.net/lulipeng_cpp/article/details/758 ...

随机推荐

  1. centos6装python3,并安装requests, lxml和beautifulsoup模块

     一.  安装python3并设为默认版本,与python2共存 1.下载Python3.4安装包 wget https://www.python.org/ftp/python/3.4.4/Pytho ...

  2. word的xml文件中给文字添加超链

    <w:hlink w:dest="http://xxx.com"><w:r></w:r></wr></w:hlink>& ...

  3. iOS开发之动画中的时间(概况)

    一.引言 在iOS开发中使用动画时,可以通过设置动画的duration.speed.begintime.offset属性,来设置动画的时长.速度.起始时间及起始偏移. 用一个简单的例子来说明各个参数的 ...

  4. jquery offset()和position()的区别

    <script src="jquery/jquery-3.3.1.min.js"></script> <script type="text/ ...

  5. Schema Workbench 启动慢

    原始是JDBC连接设定的时候需要在参数中增加下列选项 FILTER_SCHEMA_LIST 官方的解释是 就是去搜寻连接数据库的所有的表结构,表越大越慢. 也要把这选项去除,保存数据库链接,并重新登录 ...

  6. tp5 重定向缺少index.php报错(No input file specified)

    转别人的,有用,Mark一下 public 下的.htaccess 修改为 <IfModule mod_rewrite.c>  Options +FollowSymlinks -Multi ...

  7. Linux Shell脚本编程基础

    1. 脚本是一个包含一系列命令序列的文本文件,当运行这个脚本文件时,文件中包含的命令序列将得到执行. 2. 脚本主要由两部分组成:脚本解释器和命令序列 注:#!/bin/bash 指明脚本解释器为Ba ...

  8. springboot 接口返回数据时 net.sf.json.JSONNull["empty"]) 异常

    @ResetController返回数据时出现异常 Could not write JSON: Object is null; nested exception is com.fasterxml.ja ...

  9. 转 JSON在PHP中的基本应用

    PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 该函数主要用来将数组和对象,转换为json格式.先看一个数组转换 ...

  10. pip安装Python依赖环境

    将包依赖信息保存在requirements.txt文件 pip freeze > requirements.txt 根据依赖文件安装依赖 pip install -r requirements. ...