POJ 1947-Rebuilding Roads(树形背包)
题意:
一个树求得到一个节点数为p的子树,最小需要删除的边数。
分析:父节点到儿子这条边,删或不删,背包问题。
- #include <map>
- #include <set>
- #include <list>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <cstdio>
- #include <vector>
- #include <string>
- #include <cctype>
- #include <complex>
- #include <cassert>
- #include <utility>
- #include <cstring>
- #include <cstdlib>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- typedef pair<int,int> PII;
- typedef long long ll;
- #define lson l,m,rt<<1
- #define pi acos(-1.0)
- #define rson m+1,r,rt<<11
- #define All 1,N,1
- #define read freopen("in.txt", "r", stdin)
- const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
- const int INF= 0x7ffffff;
- const int mod = ;
- int dp[][],n,p,par[];
- int find(int x){
- return x==par[x]?x:par[x]=find(par[x]);
- }
- vector<int>e[];
- void dfs(int root){
- for(int i=;i<=p;++i)
- dp[root][i]=INF;
- dp[root][]=;
- for(int i=;i<e[root].size();++i){
- int son=e[root][i];
- dfs(son);
- for(int j=p;j>;--j)
- for(int k=;k<j;++k){
- if(k==)dp[root][j]=dp[root][j]+;//删除边数加1
- else
- dp[root][j]=min(dp[root][j],dp[root][j-k]+dp[son][k]);//不删背包
- }
- }
- }
- int main()
- {
- while(~scanf("%d%d",&n,&p)){
- for(int i=;i<=n;++i){
- par[i]=i;
- e[i].clear();
- }
- int a,b;
- for(int i=;i<n-;++i)
- {
- scanf("%d%d",&a,&b);
- e[a].push_back(b);
- par[b]=a;
- }
- int root=find();
- dfs(root);
- int minn=dp[root][p];
- for(int i=;i<=n;++i){
- minn=min(minn,dp[i][p]+);
- }
- printf("%d\n",minn);
- }
- return ;
- }
POJ 1947-Rebuilding Roads(树形背包)的更多相关文章
- [poj 1947] Rebuilding Roads 树形DP
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10653 Accepted: 4884 Des ...
- DP Intro - poj 1947 Rebuilding Roads(树形DP)
版权声明:本文为博主原创文章,未经博主允许不得转载. Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- POJ 1947 Rebuilding Roads 树形dp 难度:2
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9105 Accepted: 4122 ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...
- 树形dp(poj 1947 Rebuilding Roads )
题意: 有n个点组成一棵树,问至少要删除多少条边才能获得一棵有p个结点的子树? 思路: 设dp[i][k]为以i为根,生成节点数为k的子树,所需剪掉的边数. dp[i][1] = total(i.so ...
- POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..
dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t) < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...
- POJ 1947 Rebuilding Roads(树形DP)
题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...
随机推荐
- Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...
- Begin Andriod -- 安装android开发环境
很久以前学过Andriod,现在已经忘的快没有了,重新捡起来练练,顺带写写博客,感受下写博的乐趣. 第一步:安装java jdk.jre(jdk:开发环境,jre:运行环境). (一)java jdk ...
- C#和Js 编码和解码方法
Server.UrlDecode(); Server.UrlEncode(); Server.HtmlDecode(); Server.HtmlEncode();
- myeclipse2013 for linux及其破解补丁百度网盘下载
FQ下载1.1G的东西不是开玩笑的,用GA下载了两回均失败,还是用了某某门在win下下载好的,来之不易,所以特意上传分享给大家,免得FQ.破解文件也一并附上: 注意:本人这个是在原文件基础上bzip2 ...
- js中批量处理样式——cssText的使用
http://www.cnblogs.com/snandy/archive/2011/03/12/1980444.html
- Educational Codeforces Round 5 A
Problem A:http://codeforces.com/contest/616/problem/A A. Comparing Two Long Integers 果然还是我太天真了(长整数比较 ...
- hdu 3449
有依赖的背包,转化成01背包来做: #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- dijkstra,bellman-ford,floyd分析比较
http://www.cnblogs.com/mengxm-lincf/archive/2012/02/11/2346288.html 其实我一直存在疑惑是什么导致dijkstra不能处理负权图? 今 ...
- Oracle----dual
1. dual is a table that contains a single row. 2. The dual table has one VARCHAR2 column named dummy ...
- 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...