poj 1947 Rebuilding Roads解题报告

本博客探讨了一个重建道路场景下,如何通过破坏最少的路径,使得恰好P个节点形成孤立子树的问题。利用后序遍历建立树结构,并通过深度优先搜索算法计算从任意节点分离出P个节点所需的最少减边数。此过程涉及树形DP(动态规划)技巧,最终求解最小隔离子树所需的路径数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9582 Accepted: 4364

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


Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9582 Accepted: 4364

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


题目大意:通过减边得到给出结点数的子树。要注意的是并不是所有的case中1都是根,所以我们要找到根。题中给出的是有根树。
第一道树形dp,强行理解了好久。
首先建立一个树,dfs用后序遍历。
对于任意一个结点,dp[I][j]代表从结点I分离出j个结点的最少减边数。
每个点都有两种状态转移,
1.这个点包括在剩下的子树中。dp[I][j]=dp[I][j]+1
2.这个点不包括在剩下的子树中。dp[I][j]=dp[I][j-m]+dp[k][m]  k为i的子树。
这个dp计算应该叫刷表法吧?
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int father[157];
int root;
int dp[157][157];
int n, p;
struct Node                  //结点
{
    vector<int> son;
}node[157];
//typedef struct node;
void draw(int x, int y)     //建立树
{
    node[x].son.push_back(y);
}
void init()
{
    memset(father, 0, sizeof(father));
    for(int i=1;i<157;i++){
        node[i].son.clear();
    }
}
void dfs(int root)           //对于任意一个点,我们要先计算他所有的子树,因此用后序遍历
{
    int len=node[root].son.size();
    for(int i=0;i<=p;i++){
        dp[root][i]=157;        //这个数要很大(至少比能在计算中取到的要大),这个数表示这个状态不成立(计算的时候min就可以把他舍去)
    }
    dp[root][1]=0;
    if(len==0) return ;
    for(int i=0;i<len;i++){
        dfs(node[root].son[i]);
    }
    for(int j=0;j<len;j++){
        int k=node[root].son[j];
        for(int l=p;l>=1;l--){
            int t=dp[root][l]+1;
            for(int m=1;m<l;m++){
                t=min(t, dp[root][l-m]+dp[k][m]);
            }
            dp[root][l]=t;
        }
    }
    return ;
}
void solve()
{
    int ans;
    dfs(root);
    ans=dp[root][p];
    for(int i=1;i<=n;i++){        //如果答案不在根结点上要多减一条边
       ans=min(ans, dp[i][p]+1);
    }
    printf("%d\n", ans);
    return ;
}
int main()
{
    while(scanf("%d%d", &n, &p)!=EOF&&(n!=0||p!=0)){
        init();
        for(int i=1;i<n;i++){
            int x, y;
            scanf("%d%d", &x, &y);
            if(x>y) swap(x, y);
            father[y]++;
            draw(x, y);
        }
        for(int i=1;i<=n;i++){        //没有父亲的点是根
            if(!father[i]) {
                root=i;
            }
        }
        solve();
    }
}
一些测试数据可以在poj的讨论版里找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值