poj 1947 Rebuilding Roads解题报告


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的讨论版里找到。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的纺织品企业财务管理系统,源码+数据库+毕业论文+视频演示 在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行更好的维护,纺织品企业财务管理系统的出现就变得水到渠成不可缺少。通过对纺织品企业财务管理系统的开发,不仅仅可以学以致用,让学到的知识变成成果出现,也强化了知识记忆,扩大了知识储备,是提升自我的一种很好的方法。通过具体的开发,对整个软件开发的过程熟练掌握,不论是前期的设计,还是后续的编码测试,都有了很深刻的认知。 纺织品企业财务管理系统通过MySQL数据库与Spring Boot框架进行开发,纺织品企业财务管理系统能够实现对财务人员,员工,收费信息,支出信息,薪资信息,留言信息,报销信息等信息的管理。 通过纺织品企业财务管理系统对相关信息的处理,让信息处理变的更加的系统,更加的规范,这是一个必然的结果。已经处理好的信息,不管是用来查找,还是分析,在效率上都会成倍的提高,让计算机变得更加符合生产需要,变成人们不可缺少的一种信息处理工具,实现了绿色办公,节省社会资源,为环境保护也做了力所能及的贡献。 关键字:纺织品企业财务管理系统,薪资信息,报销信息;SpringBoot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值