题解:枚举最后在哪里停止,然后剩下的步数/2
也就是找最大深度
枚举终止位置算是一种思路吧
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=10009; int n,m; int maxdep; int cntedge; int head[maxn]; int to[maxn<<1],nex[maxn<<1]; void Addedge(int x,int y){ nex[++cntedge]=head[x]; to[cntedge]=y; head[x]=cntedge; } void Dfs(int x,int fa,int dep){ maxdep=max(maxdep,dep); for(int i=head[x];i;i=nex[i]){ if(to[i]==fa)continue; Dfs(to[i],x,dep+1); } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n-1;++i){ int x,y; scanf("%d%d",&x,&y); Addedge(x+1,y+1); Addedge(y+1,x+1); } Dfs(1,1,0); if(m<=maxdep)printf("%d\n",m+1); else printf("%d\n",min(n,(m-maxdep)/2+maxdep+1)); return 0; }
原文地址:https://www.cnblogs.com/zzyer/p/8457010.html
时间: 2024-10-13 09:45:02