#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 150
int N,M;
vector<int>vec[150];
queue<int>q;
int cnt[MAXN];//保存每个层次上的叶子节点个数。
int first,last,tmpcnt,level;
void BFS(){
while (!q.empty()) {
first=q.front();
int index=q.front();
q.pop();
if (vec[index].size()==0) {
tmpcnt++;
}
else
for (int i=0; i<vec[index].size(); i++)
q.push(vec[index][i]);
if (first==last) {
cnt[level]=tmpcnt;
level++;
tmpcnt=0;
last=q.back();
}
}
}
int main(){
// freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
scanf("%d%d",&N,&M);
for (int i=0; i<M; i++) {
int index,num;
scanf("%d%d",&index,&num);
for (int j=0; j<num; j++) {
int child;
scanf("%d",&child);
vec[index].push_back(child);
}
}
q.push(1);
first=q.front();
last=q.back();
level=0;
tmpcnt=0;
BFS();
for (int i=0; i<level-1; i++)
printf("%d ",cnt[i]);
printf("%d\n",cnt[level-1]);
return 0;
}
PAT 1004. Counting Leaves (30)
最新推荐文章于 2018-03-11 21:36:51 发布