我猜我这样继续做水题会狗带

和模拟赛的题很像,贪心搞一下。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int read(){
 4   int x=0,f=1;char ch=getchar();
 5   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 6   while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 7   return x*f;
 8 }
 9 #define N 100005
10 int n,m,f[N],q[N],cnt;
11 struct Node{
12   int to,next;
13 }e[N<<1];
14 int tot,head[N];
15 void add(int x,int y){
16   e[++tot]=(Node){y,head[x]};head[x]=tot;
17   e[++tot]=(Node){x,head[y]};head[y]=tot;
18 }
19 void dfs(int x,int fa,int lim){
20   f[x]=0;
21   int t=0;
22   for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa)dfs(e[i].to,x,lim);
23   for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa){
24     q[++t]=f[e[i].to]+1;
25   }
26   sort(q+1,q+1+t);
27   while(t&&q[t]+q[t-1]>lim)cnt++,t--;
28   f[x]=q[t];
29   return;
30 }
31 bool check(int x){
32   cnt=0;dfs(1,0,x);
33   if(cnt<=m)return 1;else return 0;
34 }
35 int main(){
36   n=read();m=read();
37   for(int i=1;i<n;i++)add(read(),read());
38   int l=1,r=n-1,ans,mid;
39   while(l<=r){
40     mid=l+r>>1;
41     if(check(mid))r=mid-1,ans=mid;
42     else l=mid+1;
43   }
44   printf("%d\n",ans);
45 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2021-10-30
  • 2021-11-05
  • 2022-03-07
  • 2021-11-06
  • 2021-08-17
猜你喜欢
  • 2021-08-19
  • 2022-12-23
  • 2021-12-24
  • 2022-03-03
  • 2021-09-02
  • 2021-08-14
  • 2022-03-05
相关资源
相似解决方案