JZOJ5360. 【NOIP2017提高A组模拟9.12】Shorten Diameter

Description

给定一棵有n 个点的树,现要求不断删点直到树的直径<=K,求最少需要删除的点数。
一个点可以被删掉当且仅当该点的度数为1。

Input

第一行两个数n,k,意义如题所述。
接下来n -1 行描述这棵树。

Output

一个数表示答案。

Sample Input

6 2
1 2
3 2
4 2
1 6
5 6

Sample Output

2

题解

最后保留下来的树的直径是不超过k的,
但如果是最优的情况,就尽快取到k。

我们枚举一个一定被保留的点,
先看k是偶数的情况,那么从这个点向外延伸k/2条边的都可以保留下来,
这样两边加起来就是直径k了。

如果k是奇数,对于一个点i,它的所以儿子中,只能有一个儿子向外延伸k/2+1条边,
其余的儿子都只能是k/2条边。
因为如果存在两个儿子都选了k/2+1条边,那么最终的直径就是k+1了。

code

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define ll long long
#define N 2003
#define db double
#define P putchar
#define G getchar
#define mo 10000
using namespace std;
char ch;
void read(int &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    ll w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=G();
    n*=w;
}

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}

void write(int x)
{
     if(x>9) write(x/10);
     P(x%10+'0');
}

int next[N*2],a[N*2],b[N],tot;
int n,ans,sum,mx,k,x,y,son[N];

void ins(int x,int y)
{
    next[++tot]=b[x];
    a[tot]=y;
    b[x]=tot;
}

int dfs(int x,int fa,int deep)
{
    if(deep==0)return 0;
    int s=1;
    for(int i=b[x];i;i=next[i])
        if(a[i]!=fa)s+=dfs(a[i],x,deep-1);
    return s;
}

int main()
{
    freopen("c.in","r",stdin);
    freopen("c.out","w",stdout);
    read(n);read(k);
    for(int i=1;i<n;i++)
        read(x),read(y),ins(x,y),ins(y,x);

    for(int i=1;i<=n;i++)
    {
        mx=0;
        sum=1;
        for(int j=b[i];j;j=next[j])
        {
            x=dfs(a[j],i,k/2);
            y=dfs(a[j],i,k/2+1);
            sum+=x;
            mx=max(mx,y-x);
        }
        if(k%2)sum+=mx;
        ans=max(ans,sum);
        //write(sum),P(' '),write(mx),P('\n');
    }

    write(n-ans);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值