直接深搜 ,水啊
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 100002 int head[N]; //bool vis[N]; int cnt; struct node{ int to,next; }edge[N]; int n,d,ans,dep; void init() { ans=cnt=dep=0; memset(head,-1,sizeof(head)); //memset(vis,0,sizeof(vis)); } void addedge(int u,int v) { edge[cnt].to=v ; edge[cnt].next=head[u]; head[u]=cnt++; } void dfs(int u) { if(dep>d)ans++; dep++; int k,v; for(k=head[u];k!=-1;k=edge[k].next) { v=edge[k].to; dfs(v); } dep--; } int main() { int t,u,v,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&d); init(); for(i=0;i<n-1;i++) { scanf("%d%d",&u,&v); addedge(u,v); } dfs(0); printf("%d\n",ans); } return 0; }