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

这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然而状态转移方程不太好想。

而题解其实差不多,只不过j为剩余点的个数

这样我们就有最初状态,dp[i][1] = 子节点个数,而dp[i][j]就可以从子节点的状态推出来

dp[i][j] = min(dp[i][j],dp[i][k]-1+dp[s][j-k])  (1<=i<=n,2<=j<=sum(节点总和),1<=k<j,s为i子节点)(i中已有k个节点并从s中选择j-k个,算最少删除边数,s选上所以i->s的边不需删除,所以-1)

#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 1010
#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[],dp[][],ptr=; int sum[],vl[],vis[]; 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][] = tot; 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]!=INF && dp[p][j-k]!=INF)
{
dp[root][j] = min(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(a,b);
add(b,a);
} mem(dp,INF);
mem(vis,);
dfs();
int ans = INF;
for (i = ; i <= n; ++i) { if (i == )
ans = min(ans,dp[i][m]);
else ans = min(ans,dp[i][m]+);//非根节点要删除连到父亲节点的那条边
}
printf("%d\n",ans);
}
return ;
}

poj 1947 树形背包 (删边)的更多相关文章

  1. poj 1947 树形背包

    重做这道题 http://blog.csdn.net/woshi250hua/article/details/7632785 http://blog.csdn.net/shuangde800/arti ...

  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. dataTable 从服务器获取数据源的两种表现形式

    var table = $('#example1').DataTable({ "processing": true,//加载效果 "autoWidth": fa ...

  2. python3之if与语句

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 本节主要介绍python,if条件语句,以及用法.下面附有之前的文章: 语句快介绍 语句快并非一种语句,是通过缩进形成的语句集合: 可以使用的缩进 ...

  3. selenium自动加载Flash

    当我们在定位的时候,有时候会碰到Flash问题导致无法定位到元素 通过下面的代码就能解决问题 参考:https://blog.csdn.net/qq_37913997/article/details/ ...

  4. leetcode-680-Valid Palindrome II

    题目描述: Given a non-empty string s, you may delete at most one character. Judge whether you can make i ...

  5. Django中的CSRF(跨站请求伪造)

    Django中的CSRF(跨站请求伪造) Django CSRF  什么是CSFR 即跨站请求伪装,就是通常所说的钓鱼网站. 钓鱼网站的页面和正经网站的页面对浏览器来说有什么区别? (页面是怎么来的? ...

  6. 让windows登陆界面显示administrator账户

    如果windowsXP只有一个administrator账户,在开机登陆windows的欢迎界面,会出现这个账户名,点击,输入密码就可登陆到windows桌面: 如果新建了另一个管理员账户,在欢迎界面 ...

  7. 认识CSS中高级技巧之元素的显示与隐藏

    前端之HTML,CSS(八) CSS高级技巧 元素的显示与隐藏 CSS中有三个属性可以设置元素的显示于隐藏,分别是:display.visibility和overflow. display 隐藏元素: ...

  8. css提取数据2个常用方法

    提取标签里的内容 所谓数据就是HTML里标签的内容,如下面红色字体,就是标签内容 <title>我只是个实验 - SCRAPY</title> 提取标签里的数据,标签可以是ti ...

  9. 使用docker部署STF服务(CentOS环境)

    一.安装docker环境 更新软件 sudo yum update 执行安装 sudo yum install docker 查看docker镜像 sudo docker images 二.拉取相关镜 ...

  10. 完美解决读取Excel的数字单元格时Cannot get a STRING value from a NUMERIC cell 报错处理

    我使用的是Poi(最新的4.1.0)方式读取Excel ,我的方法如下: 在打印cell内容时,抛出下面的错误 Exception in thread "main" java.la ...