7 2 1 2 1 3 2 4 2 5 3 6 3 7
2
#include <cstdio> #include <cstring> int const MAX = 105; struct EDGE { int v, next; }e[MAX]; int head[MAX], num[MAX]; bool in[MAX], vis[MAX]; int cnt, ans; int n, k; void Add(int u, int v) { e[cnt].v = v; e[cnt].next = head[u]; head[u] = cnt ++; } void DFS(int u) { vis[u] = true; num[u] = 0; for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].v; if(!vis[v]) { DFS(v); num[u] += (num[v] + 1); } } if(num[u] == k) ans ++; return; } int main() { while(scanf("%d %d", &n, &k) != EOF) { cnt = 0; ans = 0; memset(head, -1, sizeof(head)); memset(in, false, sizeof(in)); memset(vis, false, sizeof(vis)); for(int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); Add(u, v); in[v] ++; } for(int i = 1; i <= n; i++) if(in[i] == 0) DFS(i); printf("%d\n", ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/tc_to_top/article/details/47108595