-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1004.cpp
63 lines (59 loc) · 1.06 KB
/
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
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 110;
int N, M, cnt[MAXN] = {0}, maxL = -1;
vector<int> node[MAXN];
int h[MAXN];
void DFS(int index, int level){
if(node[index].size() == 0){
++cnt[level];
if(level > maxL){
maxL = level;
}
}else{
for(int i = 0; i < node[index].size(); ++i){
DFS(node[index][i], level+1);
}
}
}
void BFS(int root){
queue<int> q;
q.push(root);
h[root] = 1;
while(!q.empty()){
int front = q.front();
q.pop();
if(node[front].size() == 0){
++cnt[h[front]];
if(maxL < h[front]){
maxL = h[front];
}
}else{
for(int i = 0; i < node[front].size(); ++i){
int child = node[front][i];
q.push(child);
h[child] = h[front] + 1;
}
}
}
}
int main(){
scanf("%d%d", &N, &M);
for(int i = 0; i < M; ++i){
int id, k, child;
scanf("%d%d", &id, &k);
for(int j = 0; j < k; ++j){
scanf("%d", &child);
node[id].push_back(child);
}
}
//DFS(1, 1);
BFS(1);
for(int i = 1; i <= maxL; ++i){
if(i != 1) printf(" ");
printf("%d", cnt[i]);
}
return 0;
}