poj 1947 Rebuilding Roads (树形dp)

Rebuilding Roads
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8561   Accepted: 3831

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


题意:

给你一棵树,问最少去掉几条边,能够得到一颗节点为m的子树。


思路:

能很快找到状态 

dp[i][j]  i-节点  j-得到节点为j的子树  dp-最小去掉的边

每个孩子有取或不取两种情况,则

dp[i][j]=min(dp[son][k]+dp[i][j-k],dp[i][j]+1);   // 如果不取的话,需要将此边去掉

更新答案是注意当前节点是否为根节点。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 305
#define MAXN 400005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,k,ans,cnt,tot,flag;
int pp[maxn],dp[maxn][maxn];
struct Node
{
    int v,w,next;
} edge[maxn];

void addedge(int u,int v,int w)
{
    cnt++;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=pp[u];
    pp[u]=cnt;
}
void dfs(int u,int pre)
{
    int i,j,v,t;
    dp[u][1]=0;
    for(i=pp[u]; i; i=edge[i].next)
    {
        v=edge[i].v;
        if(v!=pre)
        {
            dfs(v,u);
            for(j=n; j>=0; j--)
            {
                t=INF;
                for(int k=1; k<=j; k++)
                {
                    t=min(t,dp[v][k]+dp[u][j-k]);
                }
                dp[u][j]=min(dp[u][j]+1,t);
            }
        }
    }
    if(u!=1) ans=min(ans,dp[u][m]+1); // 不是根节点还要去掉与上面相连的边
    else ans=min(ans,dp[u][m]);
}
int main()
{
    int i,j,t;
    while(~scanf("%d%d",&n,&m))
    {
        int u,v;
        cnt=0;
        memset(pp,0,sizeof(pp));
        for(i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v,0);
            addedge(v,u,0);
        }
        ans=INF;
        memset(dp,0x3f,sizeof(dp));
        dfs(1,0);
        printf("%d\n",ans);
    }
    return 0;
}




你可能感兴趣的:(poj 1947 Rebuilding Roads (树形dp))