标明出处:https://www.liuchuo.net/archives/2229
1004 Counting Leaves(30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 0
#include <cstdio>
#include <vector>
using namespace std;
vector<int> v[100];
int book[100], maxdepth = -1;
void dfs(int index, int depth){
if(v[index].size() == 0){
book[depth]++;
maxdepth = max(maxdepth, depth);
return ;
}
for(int i = 0; i < v[index].size(); i++){
dfs(v[index][i], depth+1);
}
}
int main(){
int n, m, k, node, c;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++){
scanf("%d %d", &node, &k);
for(int j = 0; j < k; j++){
scanf("%d", &c);
v[node].push_back(c);
}
}
dfs(1, 0);
printf("%d", book[0]);
for(int i = 1; i <= maxdepth; i++)
printf(" %d", book[i]);
return 0;
}