-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path1004.cpp
47 lines (43 loc) · 947 Bytes
/
1004.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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int leaf[100] = {0}, layer = 0; //每层存放的叶结点个数,层数
struct {
int layer = 1;
vector<int> child;
} Node[100];
void BFS(int root) {
queue<int> q;
q.push(root); //根结点入队
while(!q.empty()) {
int now = q.front();
q.pop();
layer = max(Node[now].layer, layer);
if(Node[now].child.size() == 0) //当前为叶结点
leaf[Node[now].layer]++; //对应层数的叶结点数量 + 1
for(int i = 0; i < Node[now].child.size(); i++) {
Node[Node[now].child[i]].layer = Node[now].layer + 1;
q.push(Node[now].child[i]);
}
}
}
int main() {
int N, M, ID, K;
cin >> N >> M;
for(int i = 0; i < M; i++) {
cin >> ID >> K;
int cID;
for(int j = 0; j < K; j++) {
cin >> cID;
Node[ID].child.push_back(cID);
}
}
BFS(1);
for(int i = 1; i <= layer; i++) {
if(i > 1)
cout << " ";
cout << leaf[i];
}
return 0;
}