P3698 [CQOI2017] 小Q的棋盘

/*
贪心:
1 N<=len最长链-1 ->N
2 N>len -> len+(N-(len-1))/2
*/
/*
5 2
1 0
2 1
3 2
4 3

3

9 5
0 1
0 2
2 6
4 2
8 1
1 3
3 7
3 5

5
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<queue>
#include<vector>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=1e3 +10;
const int mod=998244353;
const int inf=0x3f3f3f3f;

int n,N,mdep;
int head[maxn],to[maxn<<1],nxt[maxn<<1],tot,dep[maxn];

void add(int a,int b){
    to[++tot]=b,nxt[tot]=head[a],head[a]=tot;
}

void dfs(int u,int fa)
{
    dep[u]=dep[fa]+1;mdep=max(mdep,dep[u]);
    for(int i=head[u];i;i=nxt[i])
    {
        int v=to[i];if(v==fa) continue;
        dfs(v,u);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>N;
    for(int i=1;i<=n-1;i++){
        int a,b;cin>>a>>b;
        add(a,b),add(b,a);
    }
    dfs(0,0);
    if(N<=mdep-1) cout<<N+1<<'\n';
    else cout<<min(n,mdep+(N-mdep+1)/2)<<'\n';
    
   // for(int i=1;i<=n-1;i++) cout<<endl<<dep[i]<<endl;
    return 0;
}

 

posted @ 2023-11-12 09:06  JMXZ  阅读(5)  评论(0)    收藏  举报