POJ1947:Rebuilding Roads(树形DP)

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,p,一共有n个节点,要求最少减去最少的边是多少,剩下p个节点
思路:典型的树形DP,

dp[s][i]:记录s结点,要得到一棵j个节点的子树去掉的最少边数
 考虑其儿子k
 1)如果不去掉k子树,则
 dp[s][i] = min(dp[s][j]+dp[k][i-j])  0 <= j <= i

 2)如果去掉k子树,则
 dp[s][i] =  dp[s][i]+1
 总的为
 dp[s][i] = min (min(dp[s][j]+dp[k][i-j]) ,  dp[s][i]+1 )

 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n,p,root;
int dp[155][155];
int father[155],son[155],brother[155];

void dfs(int root)
{
    int i,j,k,tem;
    for(i = 0; i<=p; i++)
        dp[root][i] = 10000000;
    dp[root][1] = 0;
    k = son[root];
    while(k)
    {
        dfs(k);
        for(i = p; i>=1; i--)
        {
            tem = dp[root][i]+1;
            for(j = 1; j<i; j++)
                tem = min(tem,dp[k][i-j]+dp[root][j]);
            dp[root][i] = tem;
        }
        k = brother[k];
    }
}

int solve()
{
    int ans,i;
    dfs(root);
    ans = dp[root][p];
    for(i = 1; i<=n; i++)//除了根节点,其他节点要想成为独立的跟,必先与父节点断绝关系,所以要先加1
        ans = min(ans,dp[i][p]+1);
    return ans;
}

int main()
{
    int i,x,y;
    while(~scanf("%d%d",&n,&p))
    {
        memset(father,0,sizeof(father));
        memset(son,0,sizeof(son));
        for(i = 1; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            father[y] = 1;//记录该点有父亲节点
            brother[y] = son[x];//记录兄弟节点
            son[x] = y;//记录子节点
        }
        for(i = 1; i<=n; i++)
        {
            if(!father[i])//找到根节点
            {
                root = i;
                break;
            }
        }
        printf("%d\n",solve());
    }

    return 0;
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值