A1004 Counting Leaves (30point(s))
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 101;
int N, M;
struct Node{
vector<int> child;
int level;
Node(){
level = -1;
}
}node[maxn];
int maxlevel = -1;
int ans[maxn] = {0};
void BFS(int s){
node[s].level = 0;
maxlevel = 0;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
if(node[u].child.empty()) ans[node[u].level]++;
for(int i=0; i<node[u].child.size(); i++){
int v = node[u].child[i];
node[v].level = node[u].level + 1;
if(node[v].level > maxlevel) maxlevel = node[v].level;
q.push(v);
}
}
}
int main(){
scanf("%d %d", &N, &M);
int u, v, K;
for(int i=0; i<M; i++){
scanf("%d", &u);
scanf("%d", &K);
for(int j=0; j<K; j++){
scanf("%d", &v);
node[u].child.push_back(v);
}
}
BFS(1);
for(int i=0; i<=maxlevel; i++){
printf("%d", ans[i]);
if(i<maxlevel) printf(" ");
}
return 0;
}