重做这道题

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. 区块链中的密码学(四)- Merkle树和SPV节点

    什么是Merkle Tree? Merkle Tree 的命名来自于美国密码学家Ralph C. Merkle ,关于他的个人资料:传送门https://en.wikipedia.org/wiki/R ...

  2. CentOs中Apache文件访问去除index.php

    0.apache下面和index.php同一个目录下面的 .haccess <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{ ...

  3. Linq中的group by多表多字段,Sum求和

    //Line to Sql 写法 var data = (from a in Items group a by new { a.GroupId, a.Id } into b //orderby new ...

  4. 打谷机 BZOJ 1603 模拟

    Farmer John有一个过时的打谷机(收割小麦),它需要带子来带动.发动机驱动轮1总是顺时针旋转的,用来带动转轮2,转轮2来带动转轮3,等等.一共有n(2<=n<=1000)个转轮(n ...

  5. 基础线程机制--Executor线程池框架

    基础线程机制 Executor线程池框架 1.引入Executor的原因 (1)new Thread()的缺点 ​  每次new Thread()耗费性能 ​  调用new Thread()创建的线程 ...

  6. docker的常用操作

    查看所有的镜像: docker images 查看所有的容器: docker ps -a 查看正在运行的容器: docker ps 移除容器: docker rm -f 容器id 移除镜像: dock ...

  7. VS2010 简单ATL COM开发

    http://blog.csdn.net/wangwenjing90/article/details/8771934#reply http://blog.csdn.net/wangwenjing90/ ...

  8. python 批量下载 spring 的 xsd

    #coding=utf-8 import os import urllib import urllib2 import re from bs4 import BeautifulSoup # 利用 ur ...

  9. P2596 [ZJOI2006]书架(splay)

    [题目链接] https://www.luogu.org/problemnew/show/P2596 平衡树,需支持五个操作: 1. 将某元素置顶:将元素旋到根,然后将左子树合并到该元素的后继 2. ...

  10. stringstream的用法

    stringstream的基本用法 stringstream是字符串流.它将流与存储在内存中的string对象绑定起来. 在多种数据类型之间实现自动格式化. 1.stringstream对象的使用 # ...