标签:
7 2 1 2 1 3 2 4 2 5 3 6 3 7
2
题解:比如1领导2,再去看2能领导几个人,这里就可以想到用递归做。用个计数器记录1能领导(包括间接)的人数,记录下所有人能领导的人数,遍历一遍就能求出答案。
参考代码:
#include<stdio.h> #include<string.h> struct node { int num,s[105]; }res[105]; int ans; void judge(int i) { if(res[i].num==0) return ; ans+=res[i].num; for(int j=0;j<res[i].num;j++) judge(res[i].s[j]); } int main() { int n,k,a,b,cnt; while(~scanf("%d%d",&n,&k)) { memset(res,0,sizeof(res)); for(int i=0;i<n-1;i++) { scanf("%d%d",&a,&b); res[a].s[res[a].num]=b; res[a].num++; } cnt=0; for(int i=1;i<=n;i++) { ans=res[i].num; for(int j=0;j<res[i].num;j++) judge(res[i].s[j]); if(ans==k) cnt++; } printf("%d\n",cnt); } return 0; }
版权声明:本文为博主原创文章,随便转载。
标签:
原文地址:http://blog.csdn.net/luwhere/article/details/47446129