poj 1947 树形背包
重做这道题
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 树形背包的更多相关文章
- poj 1947 树形背包 (删边)
http://blog.csdn.net/woshi250hua/article/details/7632785 这道题我一开始想的dp[i][j],i是节点,j是删除的点数,dp是最少删边的个数,然 ...
- poj 1155 树形背包
http://blog.csdn.net/libin56842/article/details/9908199 树形背包: 首先是建树,每个结构体为一个节点,包括下一个点序号,值,和next. tre ...
- poj 1947(树形DP+背包)
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10663 Accepted: 4891 ...
- POJ 1155 树形背包(DP) TELE
题目链接: POJ 1155 TELE 分析: 用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理. dp[cnt][i+j] = max( dp[cnt][i+j ...
- 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 ...
- POJ 1155-TELE(树形背包)
题意:电视台发送信号给很多用户,每个用户(叶子节点)有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 分析:问题与以i为根节点的子树所包含的叶子数 #incl ...
- POJ 2486 树形背包DP Apple Tree
设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...
- UVa 1407 树形背包 Caves
这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...
- POJ 1155 (树形DP+背包+优化)
题目链接: http://poj.org/problem?id=1155 题目大意:电视台转播节目.对于每个根,其子结点可能是用户,也可能是中转站.但是用户肯定是叶子结点.传到中转站或是用户都要花钱, ...
随机推荐
- Git 分支管理-git stash 和git stash pop
https://blog.csdn.net/u010697394/article/details/56484492 合并分支,冲突是难免的,在实际协作开发中我们遇到的情况错综复杂,今天就讲两个比较重要 ...
- 【Linux】-Ubuntu常用命令吐血整理
前言 刚刚接触Linux操作系统,真的是各种艰难啊,用个什么东西都得从头开始配置,这个时候才明白从头再来是什么滋味了.自己装了数个数十几次的Centos版本的Linux系统,好不容易争气了一次,跑了起 ...
- 【转】32位plsql连接64位oracle
源地址:http://www.cnblogs.com/ymj126/p/3712727.html
- group by 两个或以上条件的分析
首先group by 的简单说明: group by 一般和聚合函数一起使用才有意义,比如 count sum avg等,使用group by的两个要素: (1) 出现在select后面的字段 要 ...
- codevs1068(dp)
题目链接: http://codevs.cn/problem/1068/ 题意: 中文题诶~ 思路: dp 用 dp[i][j][k][l] 表示取 i 个 1, j 个 2, k 个 3, l 个 ...
- [51nod1220] 约数之和(杜教筛+莫比乌斯反演)
题面 传送门 题解 嗯--还是懒得写了--这里 //minamoto #include<bits/stdc++.h> #define R register #define IT map&l ...
- apache htaccess 一个 例子
<Files ~ "^.(htaccess|htpasswd)$"> deny from all </Files> DirectoryIndex index ...
- 处女座和小姐姐(三)(数位dp)
链接:https://ac.nowcoder.com/acm/contest/329/G 来源:牛客网 题目描述 经过了选号和漫长的等待,处女座终于拿到了给小姐姐定制的手环,小姐姐看到以后直呼666! ...
- Educational Codeforces Round 13 A
Description Johny likes numbers n and k very much. Now Johny wants to find the smallest integer x gr ...
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
Problem Description There is a collector who own many valuable jewels. He has a problem about how to ...