poj1947 Rebuilding Roads

来源:互联网 发布:w3cschool mysql 编辑:程序博客网 时间:2024/05/16 14:55
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 10297 Accepted: 4701

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 61 21 31 41 52 62 72 84 94 104 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个节点所要割断的最小边数(其中得到的子树中i为根节点),

那么对于它的每一个子树,有两种情况,一种是直接割断,那么dp[u][j]=dp[u][j]+1,第二种是不割断,那么dp[u][j]=min{dp[u][j],dp[u][j-k]+dp[v][k]};


#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;typedef long long ll;#define inf 99999999#define maxn 152int dp[maxn][maxn],vis[maxn],first[maxn],num[maxn];struct node{    int to,next;}e[2*maxn];int n,m;void dfs(int u){    int i,j,v,flag=0,k;    vis[u]=1;    for(i=first[u];i!=-1;i=e[i].next){        v=e[i].to;        if(vis[v])continue;        flag=1;        dfs(v);        for(j=m;j>=1;j--){            dp[u][j]=dp[u][j]+1;            for(k=num[v];k>=1;k--){ //这里也可以写成for(k=j-1;k>=1;k--),大多数题解都是从j-1开始,还以为不能用子节点的个数做,后来发现是子节点的个数算错了= =                if(j>k)                    dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k] );            }        }        num[u]+=num[v]; //num[i]表示以i为根节点的子节点的个数    }    num[u]++;    if(flag==0){        dp[u][1]=0;    }}int main(){    int i,j,T,c,d,tot;    while(scanf("%d%d",&n,&m)!=EOF)    {        tot=0;        memset(first,-1,sizeof(first));        for(i=1;i<=n-1;i++){            scanf("%d%d",&c,&d);            tot++;            e[tot].next=first[c];e[tot].to=d;            first[c]=tot;            tot++;            e[tot].next=first[d];e[tot].to=c;            first[d]=tot;        }        memset(vis,0,sizeof(vis));        memset(num,0,sizeof(num));        for(i=1;i<=n;i++){            dp[i][1]=0;            for(j=2;j<=m;j++){                dp[i][j]=inf;            }        }        dfs(1);        int ans=dp[1][m];        for(i=2;i<=n;i++){            ans=min(ans,dp[i][m]+1);        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击