重做这道题

http://blog.csdn.net/woshi250hua/article/details/7632785

http://blog.csdn.net/shuangde800/article/details/10150305

http://blog.csdn.net/alps233/article/details/51190997

#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 500+5
#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 #define ls (rt<<1)
#define rs (rt<<1|1) int n,m; int head[MAXN],vis[MAXN],ptr=; int dp[MAXN][MAXN],num[MAXN]; struct node{int y,next,val;}tree[MAXN<<]; void init()
{
mem(head,-);
mem(vis,);
mem(dp,INF);
mem(num,);
ptr = ;
} void add(int son,int fa)
{
tree[ptr].y=son;
tree[ptr].next=head[fa];
head[fa]=ptr++;
} void dfs(int rt)
{
num[rt] = vis[rt] = ; int tot = ; for(int i = head[rt];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
dfs(y);
tot++;
num[rt]+=num[y];
} dp[rt][] = tot; for(int i = head[rt];i!=-;i=tree[i].next)
{
int y = tree[i].y;
for(int j = num[rt];j>;j--)
{
for(int k = ;k<j;k++)
{
if(dp[rt][j-k]!=INF && dp[y][k]!=INF);
dp[rt][j] = min(dp[rt][j],dp[rt][j-k]+dp[y][k]-);
}
}
}
} int main()
{
int i,j,k=;
while(~sf("%d%d",&n,&m))
{
init();
for(i=;i<n;i++)
{
int x,y;
sf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs();
int ans = INF;
for(i=;i<=n;i++)
{
if(i==) ans = min(dp[i][m],ans);
else ans = min(dp[i][m]+,ans);
}
pf("%d\n",ans);
}
}

poj 1947 树形背包的更多相关文章

  1. poj 1947 树形背包 (删边)

    http://blog.csdn.net/woshi250hua/article/details/7632785 这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然 ...

  2. poj 1155 树形背包

    http://blog.csdn.net/libin56842/article/details/9908199 树形背包: 首先是建树,每个结构体为一个节点,包括下一个点序号,值,和next. tre ...

  3. poj 1947(树形DP+背包)

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10663   Accepted: 4891 ...

  4. POJ 1155 树形背包(DP) TELE

    题目链接:  POJ 1155 TELE 分析:  用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理.        dp[cnt][i+j] = max( dp[cnt][i+j ...

  5. poj 1947 树形dp

    思路:dp[i][j]表示,以i节点为根,删去j个节点最少要断几条边. 那么dp[u][j]=min(dp[u][j],dp[v][k]+dp[u][j-k]);//选取最优状态 dp[u][j]=m ...

  6. POJ 1155-TELE(树形背包)

    题意:电视台发送信号给很多用户,每个用户(叶子节点)有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 分析:问题与以i为根节点的子树所包含的叶子数 #incl ...

  7. POJ 2486 树形背包DP Apple Tree

    设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...

  8. UVa 1407 树形背包 Caves

    这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...

  9. POJ 1155 (树形DP+背包+优化)

    题目链接: http://poj.org/problem?id=1155 题目大意:电视台转播节目.对于每个根,其子结点可能是用户,也可能是中转站.但是用户肯定是叶子结点.传到中转站或是用户都要花钱, ...

随机推荐

  1. D. 代码填空:LIS

    LIS是最长上升子序列.什么是最长上升子序列? 就是给你一个序列,请你在其中求出一段最长严格上升的部分,它不一定要连续. 就像这样:22, 33, 44, 77 和 22, 33, 44, 66 就是 ...

  2. Java 大数相乘、大数相加、大数相减

    思路来源:: https://blog.csdn.net/lichong_87/article/details/6860329 /** * @date 2018/6/22 * @description ...

  3. 微信网页授权操作逻辑封装-C#实例

    http://blog.csdn.net/u011127019/article/details/52650619

  4. 【STL基础】list

    list 构造函数: //default: list<T> l; //空的list //fill: list<T> l(n); //n个元素, 元素默认初始化 list< ...

  5. js 弹性菜单

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. Github如何在Linux系统下创建本地仓库

    一.电脑上安装 Git Ubuntu安装GIt:  apt-get install git 查看版本信息:    git version 配置Git用户信息  输入: git config --glo ...

  7. AngularJs 中使用OpenLayer例子,手机端地图加载不显示问题

      var map, toolip, overlay;            var layer = new ol.layer.Vector();            var imgLayer = ...

  8. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_类型的各种成员

    [Class中,可能包含的成员] 常量, 字段, 实例构造器, 类型构造器, 方法, 操作符重载, 转换操作符, 属性, 事件, 类型(Class)

  9. P1060 开心的金明(动态规划背包问题)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱 ...

  10. 自动截取sql并合并,生成执行HQL

    ### 提取SQL语句 FILE_PATH="/data/SCRIPT" cat tables | while read file do echo "-----> ...