poj 1947(树形DP+01背包)

Rebuilding Roads

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 10663   Accepted: 4891

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

题意:在n个结点n-1条边的树中取m个点所需要的最少切割数.

分析:dp[u][i]代表以u为根节点的子树要得到i个结点的子树需要最少的切割数 如果考虑u的子树v,如果我们在父亲树中取k个点,那么在子树中取i-k个点
dp[u][i] = min(dp[u][k],dp[v][i-k]) ........1
如果不考虑v,那么我们只需要一刀将子树k与父亲分开即可dp[u][i] = dp[u][i]+1; ..........2
综上述:dp[u][i] = min(1,2)
我们在考虑u的时候,等于u是一个容量为m(m为背包容量)的背包,在子树中取m个结点组成,每个点只有取或不取,所以可以将其看成01背包。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#define N 155
using namespace std;

int head[N];
struct Edge{
    int u,v,next;
}edge[N];

int indegree[N];
void addEdge(int u,int v,int &k){
    edge[k].u = u,edge[k].v = v;
    edge[k].next = head[u],head[u]=k++;
}
int n,m;
int dp[N][N];///dp[u][i]代表以u为根节点的子树要得到i个结点的子树需要最少的切割数
/// 如果考虑u的子树v,如果我们在父亲树中取k个点,那么在子树中取i-k个点
///dp[u][i] = min(dp[u][k],dp[v][i-k])
///如果不考虑v,那么我们只需要一刀将子树k与父亲分开即可 dp[u][i] = dp[u][i]+1;
///综上述:dp[u][i] = min(min(dp[u][k],dp[v][i-k]),dp[u][i]+1)
///我们在考虑u的时候,等于u是一个容量为m(m为背包容量)的背包,在子树中取m个结点组成,每个点只有取或不取,所以
///可以将其看成01背包
void dfs(int u){
    for(int i=0;i<=m;i++) dp[u][i]=999999;
    dp[u][1]=0; ///初始化只取自己一个点
    for(int k = head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v;
        dfs(v);
        for(int j=m;j>=1;j--){ ///逆序枚举
            dp[u][j]+=1; ///不取子树时
            for(int k=1;k<j;k++){ ///父亲树上取得点
                int t = j-k; ///子树上取的点
                dp[u][j] = min(dp[u][k]+dp[v][t],dp[u][j]);
            }
        }
    }
}
int main()
{

    while(scanf("%d%d",&n,&m)!=EOF){
        memset(indegree,0,sizeof(indegree));
        memset(head,-1,sizeof(head));
        int tot = 0;
        int u,v;
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            addEdge(u,v,tot);
            indegree[v]++;
        }
        int root ;
        for(int i=1;i<=n;i++) if(indegree[i]==0){root = i;break;}
        //printf("%d\n",root);
        dfs(root);
        int ans = dp[root][m];
        for(int i=1;i<=n;i++){ ///加一是因为父亲结点和它之间还有边连着
            if(dp[i][m]+1<ans) ans = dp[i][m]+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-04 12:29:25

poj 1947(树形DP+01背包)的相关文章

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna

POJ 1947 树形DP入门题

给出N个点,N-1个关系,建出树形图,问最少减去几个边能得到节点数为P的树.典型树形DP题 dp[cur][j] :记录cur结点,要得到一棵j个节点的子树去掉的最少边数 转移方程用的背包的思想 对当前树的每一个子树进行计算 砍掉此子树:   dp[cur][j]=dp[cur][j]+1; 不砍掉:           for (l=0;l<=j;l++)  dp[cur][j]=Min(dp[cur][j],dp[cur][l]+dp[next][j-l]); 枚举从该树中留l个节点其他由新

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

poj 1947(树形dp)

题意:一棵树上问你最少切掉几条边使得能分割出一个结点数正好为k的子树. 思路:dp[i][j]表示以i为根切掉j个结点最少要几条边. dp[v][j] = min(dp[v][j], dp[v][j-k] + dp[x][k]); 代码如下: 1 dp[v][j] = min(dp[v][j], dp[v][j-k] + dp[x][k]); 2 } 3 } 4 } 5 } 6 } 7 return vex[v]; 8 } 9 10 int main() 11 { 12 // freopen("

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

hdu 1561 树形dp+0-1背包

1 /* 2 根据先后关系,可以建立一棵树 3 dp[i][j]表示第i个节点选j个的最大值 4 dp[i][j]=max(sigma(dp[c[i][ki]])) 5 sigma(dp[c[i][ki]])表示从i的儿子节点中一共选取j个点的最大值 6 */ 7 /*#include <iostream> 8 #include <cstdio> 9 #include <cstring> 10 #include <vector> 11 using names

hihoCoder1055.刷油漆——树形Dp+01背包

http://hihocoder.com/problemset/problem/1055 一棵有根树,包含根节点1,选择M个连续的节点,使得权值最大 dp[u][j] 表示以i为根的子树中,选择包含根节点的j个连续节点所能获得的最大权值 枚举子节点选择的个数,儿子节点选择的个数,当做01背包 #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include

hihoCoder#1055 : 刷油漆 (树形DP+01背包)

题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max(dp(u,m),dp(u,m-k)+dp(son,k)),其中0<=k<m.这是01背包,k应该从大往小枚举. 代码如下: # include<iostream> # include<cstdio> # include<cmath> # include<v

hdu 1561 The more, The Better(树形dp+01背包)

The more, The Better Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡.你能帮ACboy算出要获得