【POJ - 1947】Rebuilding Roads (树形dp,背包问题,树形背包dp)

题干:

The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree. 

Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.

Input

* Line 1: Two integers, N and P 

* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads. 

Output

A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated. 

Sample Input

11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11

Sample Output

2

Hint

[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.] 

题目大意:

将一棵n个节点的有根树,删掉一些边变成恰有m个节点的新树。求最少需要去掉几条边。

解题报告:

dp[i][j]代表,以 i 为根节点,砍掉 j 个子节点的子树,所要删除的边的个数

 AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 555;
const int INF = 0x3f3f3f3f;
int dp[MAX][MAX];//以i节点为根节点,切掉了j个节点的最小刀数 
int n,p;
vector<int> vv[MAX];
void dfs(int cur,int rt) {int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];if(v == rt) continue;dfs(v,cur);for(int j = p; j>1; j--) {for(int k = 1 ; k < j ; ++k)//划分成子树下的节点和本身的节点数dp[cur][j] = min(dp[cur][j] , dp[v][k]+dp[cur][j-k]-2);}}
}
int main()
{cin>>n>>p;for(int a,b,i = 1; i<=n-1; i++) {scanf("%d%d",&a,&b);vv[a].pb(b);vv[b].pb(a);}memset(dp,INF,sizeof dp);for(int i = 1; i<=n; i++) dp[i][1] = vv[i].size();dfs(1,-1);int ans = INF;for(int i=1;i<=n;i++) ans=min(dp[i][p],ans);cout <<ans <<endl;return 0 ;
} 

AC代码2:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,p,k,head[160],rd[160],dp[160][160],siz[160],ans=0x3f3f3f;
struct edge {int to,next;} ed[330];
void adde(int u,int v){ed[++k].to=v;ed[k].next=head[u];head[u]=k;}void dfs1(int u,int fa)
{siz[u]++;for(int i=head[u];i;i=ed[i].next){int v=ed[i].to;if(v==fa) continue;dfs1(v,u);siz[u]+=siz[v];}dp[u][siz[u]]=1;//如果切掉整棵子树,只需切掉与父节点的连边 dp[u][0]=0;//不切 
}void dfs2(int u,int fa)
{for(int i=head[u];i;i=ed[i].next){int v=ed[i].to;if(v==fa) continue;dfs2(v,u);for(int j=siz[u]-1;j;j--)//我切多少 for(int k=0;k<=j;k++)//我的儿子切多少 dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]);//我切j个节点切的最少边数=min 我切j-k个节点的最少边数+我的儿子切k个节点的最少边数 }if(siz[u]-p>=0)//有那么多节点 ans=min(ans,dp[u][siz[u]-p]+dp[u][siz[u]]);//min 切掉siz[u]-p个节点(剩P个节点)的最少边数+分开我与父亲的边 
}int main()
{scanf("%d%d",&n,&p);memset(dp,0x3f3f3f,sizeof(dp));//初始化为最大值 for(int i=1;i<=n-1;i++){int u,v;scanf("%d%d",&u,&v);adde(u,v);adde(v,u);}dfs1(1,1);//求siz数组 & 初始化 dp[1][siz[1]]=0;//根节点不需要与父节点分开  (没有父亲。。。) dfs2(1,1);//树形dp printf("%d",ans);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440594.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

seo vue 动态路由_基于vue.jsvue-router的动态更新TDK(SEO优化)

本文基于工作项目开发&#xff0c;做的整理笔记前几天帮朋友解决这个问题&#xff0c;顺带学习了一下&#xff0c;做个笔记Mark下。前提条件&#xff1a;你已经了解并使用vue&#xff0c;能够搭建应用站点。编码环境&#xff1a;system&#xff1a;OS X EI Capitan 10.13.3npm&…

*多叉树的树形背包常见建模方法

一.多叉树变二叉树。 这个技巧其实也有两种具体的方法&#xff1a;树的孩子兄弟表示法与dfs序法。 1.树的孩子兄弟表示法。 大家在学习树形结构时一定接触了一个多叉树变二叉树的方法&#xff0c;就是把每个点与它的第一个儿子连边&#xff0c;然后将它的儿子依次连接起来。可…

python把英语句子成分字母_求一个可以分析英语句子成分的软件或网站

目前还没有&#xff0c;最好的翻译软件都不能翻译英语语法&#xff0c;只能翻译语法结构简单的句子&#xff0c;更别提专业分析句子成分的软件了&#xff1b;出现专业分析英语句子成分的软件&#xff0c;英语老师应该就失业了。求一个可以分析英语句子成分的软件或网站句子是按…

【51nod - 1076】2条不相交的路径(Tarjan无向图判环)

题干&#xff1a; 给出一个无向图G的顶点V和边E。进行Q次查询&#xff0c;查询从G的某个顶点V[s]到另一个顶点V[t]&#xff0c;是否存在2条不相交的路径。&#xff08;两条路径不经过相同的边&#xff09; &#xff08;注&#xff0c;无向图中不存在重边&#xff0c;也就是说…

python 导入csv文件到oracle_python将文件夹下的所有csv文件存入mysql和oracle数据库

#oracle版首先新建python脚本(d:/python/orclImport.py)import os #引入os包if __name__ __main__:#遍历文件夹下所有的dmp文件&#xff0c;其中filename为所有文件、文件夹的名称。#因为我文件夹下确定都是dmp文件&#xff0c;所以无需进行特殊判断for filename in os.listdi…

createform用法_vue自定义表单生成器form-create使用详解

介绍form-create 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的表单生成器。并且支持生成任何 Vue 组件。结合内置17种常用表单组件和自定义组件&#xff0c;再复杂的表单都可以轻松搞定。文档 | github演示项目: 开源的高品质微信商城功能自定义组件可生…

【蓝桥杯官网试题 - 历届试题】发现环(dfs+并查集,或无向图tarjan判环,无向环,或拓扑排序)

题干&#xff1a; 问题描述 小明的实验室有N台电脑&#xff0c;编号1~N。原本这N台电脑之间有N-1条数据链接相连&#xff0c;恰好构成一个树形网络。在树形网络上&#xff0c;任意两台电脑之间有唯一的路径相连。   不过在最近一次维护网络时&#xff0c;管理员误操作使得某…

倍福 在 vs 里 编程 是怎么做到的_截图里的文字要改,字体怎么做到一模一样?...

大家是不是碰到过这么一个尴尬的情况&#xff1a;一个截图里有一个错别字&#xff0c;或者其中一小段文字要修改&#xff0c;可是苦于找不到源文件&#xff0c;又没办法找出和原来文字一模一样的字体。每次遇到这样的情况你都是怎么处理的呢&#xff1f;火箭君今天给大家介绍一…

【ZOJ - 3591】Nim(博弈问题,思维,STLmap)

题干&#xff1a; Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. The game ends when one of the players is unable to remove object in his/her turn. This player will then lose. On each turn, a pla…

【牛客 - 369B】小A与任务(贪心,优先队列)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/369/B 来源&#xff1a;牛客网 小A手头有 n 份任务&#xff0c;他可以以任意顺序完成这些任务&#xff0c;只有完成当前的任务后&#xff0c;他才能做下一个任务 第 i 个任务需要花费 xixi 的时间…

word硬回车是怎么产生的_在word中怎样删除软硬回车?

1.点击“开始”&#xff0d;“程序”&#xff0d;“附件”&#xff0d;“记事本”&#xff0c;打开“记事本”&#xff0c;放在一边。2.打开需要调整的Word文档&#xff0c;在文章的任意处单击一下鼠标左键。然后按住键盘的“Ctrl”键不要松&#xff0c;再按一下“A”键&#x…

*【POJ - 3659】Cell Phone Network (树形dp,最小支配集)

题干&#xff1a; Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so the…

测试jdbc连mysql数据库_java连接mysql数据库及测试是否连接成功的方法

本文实例讲述了java连接mysql数据库及测试是否连接成功的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;package com.test.tool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import…

mysql配置日志老化配置_mysql配置-日志大小限制和自动删除

线上的项目磁盘消耗问题, 发现和MySQL日志有关系.需要处理的问题如何限制大小 不让日志无限膨胀?配置日志不留?删除的方式和直接删除会对服务有什么影响?解决方式限制大小, 保留最近一段时间日志.set global expire_logs_days7; # 命令行进入MySQL中, 临时设置保留最近7天日…

【HDU - 5091】Beam Cannon(线段树,扫描线)

题干&#xff1a; Recently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on t…

mysql的传播特性_spring事务传播特性和mysql事务隔离级别

spring事务的传播特性--7种REQUIRED支持当前事务&#xff0c;如果没有事务会创建一个新的事务SUPPORTS支持当前事务&#xff0c;如果没有事务的话以非事务方式执行MANDATORY(强制性)支持当前事务&#xff0c;如果没有事务抛出异常REQUIRES_NEW创建一个新的事物并挂起当前事务NO…

【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)

题干&#xff1a; Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently. Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both si…

【HDU - 5090】Game with Pearls (匈牙利算法,二分图匹配)

题干&#xff1a; Tom and Jerry are playing a game with tubes and pearls. The rule of the game is: 1) Tom and Jerry come up together with a number K. 2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube i…

qt同时连接oracle和mysql_QT连接Oracle和Mysql的详细步骤,已成功~!

近几天一直在整QT的数据库连接这一块。因为QT是开源的&#xff0c;所以涉及的连接Oracle及Mysql的驱动都必须自己编译生成。通过不断的测试、调试&#xff0c;终于把QT连接Oracle和Mysql的驱动编译生成好了。QT环境&#xff1a;Qt 4.6.0打开Qt Command Prompt&#xff0c;开始菜…

【POJ - 2632】Crashing Robots(模拟)

题干&#xff1a; In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occup…