码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1947(树形DP+01背包)

时间:2016-04-21 18:03:36      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

poj 1947(树形DP+01背包)

标签:

原文地址:http://www.cnblogs.com/liyinggang/p/5418016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!