-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1004.cpp
53 lines (48 loc) · 1003 Bytes
/
A1004.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//计算叶子节点的个数
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 110;
//n是叶节点的个数,m是非叶子节点的个数
int n,m,max_level = -1;
vector<int> node[maxn];
int cts[maxn] = {0};
//根节点的实际情况是从01开始
void init() {
scanf("%d %d",&n,&m);
int id,k,child;
for(int i=0;i<m;i++){
scanf("%d %d",&id,&k);
for(int j =0;j<k;j++) {
scanf("%d",&child);
node[id].push_back(child);
}
}
}
//special
//The input ends with N being 0. That case must NOT be processed.
void DFS(int index,int level){
if(node[index].size() == 0){
cts[level]++;
if(level > max_level) max_level = level;
//到死胡同了,要返回啊
return;
}
else if(node[index].size() != 0){
for(int j=0;j<node[index].size();j++) {
DFS(node[index][j],level+1);
}
}
}
int main(){
init();
//输出的东西是每一层的叶子节点的个数,每层都要输出
DFS(1,0);
for(int i=0;i<=max_level;i++){
printf("%d",cts[i]);
if(i != (max_level)){
printf(" ");
}
}
}