@[一大](/user/79543)
```cpp
#include<bits/stdc++.h>
using namespace std;
const int m=200500;
int n,d;
struct edge{
int next,to;
}a[m];
int head[m],cnt;
void link(int u,int v){
a[++cnt].next=head[u];
head[u]=cnt;
a[cnt].to=v;
}
int v[m],dis[m];
int dfs(int x,int fa){
int tot=1;
if(v[x]==d) return tot;
for(int i=head[x];i;i=a[i].next){
int y=a[i].to;
if(y==fa) continue;
if(!v[y]){
v[y]=v[x]+1;
tot+=dfs(y,x);
}
}
return tot;
}
int main(){
cin>>n>>d;
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
link(u,v);
link(v,u);
}
v[1]=0;
cout<<dfs(1,0)-1;
}
```
改成这样就能过了
by VIOLET__FOREVER @ 2023-08-31 20:42:06
@[一大](/user/79543) 你这个代码可能会往回走,增加一个 $fa$ 记录走向他的点,保证不走回去就行
by VIOLET__FOREVER @ 2023-08-31 20:43:07
@[一大](/user/79543) 如果觉得我说的还不错的话,可否给个关注呀
by VIOLET__FOREVER @ 2023-08-31 20:43:42
@[VIOLET__FOREVER](/user/422387) 过了。多谢指点。
by EMaK @ 2023-10-15 03:24:46