1004 Counting Leaves
大致题意就是给出一棵树,求出每一层叶子节点个数。
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 6 const int maxn = 200; 7 vector<int> node[maxn]; 8 int hashtable[maxn] = {0},maxlevel = -1; 9 void DFS(int root,int level) { 10 if(node[root].size() == 0) { //叶子节点 11 hashtable[level]++; 12 maxlevel = max(maxlevel,level); 13 return ; 14 } 15 for(int i = 0; i < node[root].size(); ++i) 16 DFS(node[root][i],level+1); 17 } 18 19 int main() { 20 int n,m,parent,k,child; 21 cin>>n>>m; 22 for(int i = 0 ; i < m; ++i) { 23 cin>>parent>>k; 24 for(int j = 0; j < k; ++j) { 25 cin>>child; 26 node[parent].push_back(child); 27 } 28 } 29 DFS(1,1); 30 for(int i = 1; i <= maxlevel; ++i) { 31 if(i > 1) printf(" "); 32 printf("%d",hashtable[i]); 33 } 34 return 0; 35 }