首页 > 其他分享 >(树形DP+背包)POJ1947Rebuilding Roads

(树形DP+背包)POJ1947Rebuilding Roads

时间:2023-02-07 17:01:52浏览次数:62  
标签:fa POJ1947Rebuilding int DP Roads include root 节点 dp


Rebuilding Roads

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 13307

 

Accepted: 6171

Description

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.] 

Source

​USACO 2002 February​

代码实现:

题意:

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

分析:

定义状态dp(root,k)表示在以root为根节点的子树中,删掉一些边变成恰有k个节点的新树需要删去的最少边数。

对于根节点root的某个儿子son,

要么将son及其所有的子节点全部删掉,则dp(root,k)=dp(root,k)+1,只需删除root与son之间的边;

要么在son的子树中选出一些边删掉,构造出有j个节点的子树,状态转移方程为dp(root,k)=max(dp(root,k),dp(son,j)+dp(root,k-j))。

 

算法分析:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=310;
struct node
{
int v;///终端点
int next;///下一条同样起点的边号
int w;///权值
}edge[N*2];///无向边,2倍
int head[N];///head[u]=i表示以u为起点的所有边中的第一条边是 i号边
int tot; ///总边数
int minn;
void add(int u,int v)
{
edge[tot].v=v;
//edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
int n,m;
int dp[N][N],fa[N],num[N];
void dfs(int u,int fa)
{
num[u]=1;///记录u的子节点数量
for(int i = 0; i <= m; i++) ///初始化为无穷大
dp[u][i] = 1e18;

dp[u][0]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(fa==v) continue; ///如果下一个相邻节点就是父节点,则证明到底层了,开始递归父节点的兄弟节点

dfs(v,u);
num[u]+=num[v];
for(int j=min(num[u],m);j>=1;j--)
for(int k=0;k<(num[v],j);k++)
{
if(k!=0)
dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]);
else
dp[u][j]=dp[u][j]+1;
}
}

}
int main()
{
while(~scanf("%d%d",&n,&m))
{

memset(head,-1,sizeof(head));
//memset(dp,0x3f,sizeof(dp));
memset(fa,-1,sizeof(fa));
tot=0;

for(int i=1;i<n;i++)
{
int u,v;
num[i]=1;
scanf("%d%d",&u,&v);
add(u,v);
fa[v]=u;

}
int root=1;
for(root=1;fa[root]!=-1;root=fa[root]);//寻找根节点

dfs(root,-1);
int ans = dp[root][m];
for(int i = 1; i <= n; i++) ///除了根节点,其他节点要想成为独立的根,必先与父节点断绝关系,所以要先加1
ans = min(ans, dp[i][m] + 1);
printf("%d\n",ans);

}

return 0;
}

 

标签:fa,POJ1947Rebuilding,int,DP,Roads,include,root,节点,dp
From: https://blog.51cto.com/u_14932227/6042478

相关文章

  • 树形DP依赖背包 洛谷 P2015 二叉苹果树
    题目描述有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点)这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1。我们用一根树枝两端连接的......
  • 树形DP (cf 219D Choosing Capital for Treeland)
    题意翻译题目描述Treeland国有n个城市,这n个城市连成了一颗树,有n-1条道路连接了所有城市。每条道路只能单向通行。现在政府需要决定选择哪个城市为首都。假如城市i成为了首都......
  • 树形DP+概率DP
    动态规划报告树形dp树形DP,即在树上进行的DP。由于树固有的递归性质,树形DP一般都是递归进行的。一般需要在遍历树的同时维护所需的信息以一道题目为例2022CCPC桂林......
  • PHP远程操纵WordPress的方法(流程剖析)
    一直想写一个给wordpress群发文章的应用,这样我就能自动采集文章,然后写个脚本自动发送文章了,哈哈。虽然用python干这种事情貌似更加擅长,但是我想做个web界面访问该应用,而pyth......
  • DPU,PPM,DPMO,DPO,RTY-质量工程师不可不知的度量指标
    质量工程师经常都会接触到一些术语,其中最常见而又最易令他们混淆的,应该就是DPU、PPM(DPPM)、DPMO、DPO和RTY了。其实在进行质量改善或者六西格玛项目时,准确地测量过程性能指标......
  • 最小生成树 Kruskal算法 HDU 1102 Constructing Roads
    ProblemATimeLimit:2000/1000ms(Java/Other)   MemoryLimit:65536/32768K(Java/Other)TotalSubmission(s):13   AcceptedSubmission(s):5ProblemDes......
  • HDU/HDOJ 2067 小兔的棋盘 DP/卡特兰数
    HDU/HDOJ2067小兔的棋盘小兔的棋盘TimeLimit:1000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):12782    Acce......
  • POJ 3625 Building Roads(最小生成树+卡输出精度)
    BuildingRoadsTimeLimit: 1000MS MemoryLimit: 65536KTotalSubmissions: 13247 Accepted: 3661DescriptionFarmerJohnhadjustacquiredseveralnewfarms!He......
  • 2019南昌网络赛 I. Max answer(DP或者单调栈+思维)
    Alicehasamagicarray.Shesuggeststhatthevalueofaintervalisequaltothesumofthevaluesintheinterval,multipliedbythesmallestvalueinthein......
  • 51nod 2484 小b和排序(DP)
    小b有两个长度都为n的序列A,B。现在她需要选择一些i,然后交换A[i]和B[i],使得A和B都变成严格递增的序列。你能帮小b求出最少交换次数吗?输入保证有解。 收起输入第一行输入一......