Description
Input
Output
Sample Input
7 2 1 2 1 3 2 4 2 5 3 6 3 7
Sample Output
2
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<cstring> using namespace std; const int MAXN = 1e2+10; vector<int> node[MAXN]; int deg[MAXN]; int ma[MAXN]; int dfs(int u) { if(node[u].size() == 0) { ma[u] = 0; return ma[u] + 1; } for(int i = 0; i < node[u].size(); i++) ma[u] += dfs( node[u][i] ); return ma[u] + 1; } int main() { int n, k; while(scanf( "%d%d", &n, &k ) == 2) { for(int i = 0; i < MAXN; i++) node[i].clear(); memset( deg, 0, sizeof deg ); memset( ma, 0, sizeof ma ); int a, b,root; for(int i = 0; i <n-1; i++) { scanf( "%d%d", &a, &b ); node[a].push_back( b ); deg[b]++; } for(int i = 1; i <= n; i++) { if(deg[i] == 0) { root = i; break; } } dfs( 1 ); int ans = 0; for(int i = 1; i <= n; i++) { if(ma[i] == k) ans++; } printf( "%d\n", ans ); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/maxichu/article/details/48026647