/
pat1004.cpp
executable file
·71 lines (64 loc) · 1.09 KB
/
pat1004.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
64
65
66
67
68
69
70
71
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define SIZE 102
vector<int> tree[SIZE];
struct node{
int id;
int depth;
};
node queue[SIZE];
int res[SIZE];
int bfs(int root){
int head = 0,tail = 0;
node n;
n.id = root;
n.depth = 0;
queue[tail++] = n;
int ret = 0;
while(head < tail){
n = queue[head++];
if(n.depth> ret) ret = n.depth;
if(tree[n.id].size()){
for(int i=0;i<tree[n.id].size();i++){
node v;
v.id = tree[n.id][i];
v.depth = n.depth + 1;
queue[tail++] = v;
}
}
else{
res[n.depth]++;
}
}
return ret;
}
int col[SIZE];
int main()
{
int N,M;
scanf("%d%d",&N,&M);
for(int i=0;i<M;i++){
int id,num;
scanf("%d%d",&id,&num);
if(col[id]==0) col[id] = 1;
for(int j=0;j<num;j++){
int c;
scanf("%d",&c);
col[c] = 2;
tree[id].push_back(c);
}
}
int root = 0;
for(int i=0;i<100;i++)
if(col[i]==1){
root = i;
break;
}
int depth = bfs(root);
printf("%d",res[0]);
for(int i=1;i<=depth;i++)
printf(" %d",res[i]);
puts("");
}