代码:
#include<cstdio> #include<cstring> using namespace std; int n,k; int father[105],son[105]; int dd; void Find(int a) { //int r=son[a]; while(father[a]!=a) { a=father[a]; son[a]+=(dd); //r=son[a]; } //father[r]=a; //return a; } void Union(int a,int b) { father[b]=a; son[a]+=(son[b]+1); dd=son[b]+1; Find(a); } int main() { while(scanf("%d%d",&n,&k)==2) { for(int i=1; i<=n; i++) { father[i]=i; son[i]=0; } for(int i=1; i<n; i++) { int a,b; scanf("%d%d",&a,&b); Union(a,b); } int ans=0; for(int i=1; i<=n; i++) { //printf("%d %d ",i,son[i]); if(son[i]==k) ans++; } printf("%d ",ans); } return 0; }