树的层序遍历:
//
// main.cpp
// test
//
// Created by Jacky Roth on 2019/2/22.
// Copyright © 2019 Jacky Roth. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=105;
vector<int>node[maxn];
int N,M;
vector<int>res;
void level_travel(int index){
queue<int>q;
int now=1,next=0,top,count_l=0;
q.push(index);
while (!q.empty()) {
top=q.front();
q.pop();
now--;
if (node[top].size()==0) {
count_l++;
}else{
for (int i=0; i<node[top].size(); i++) {
q.push(node[top][i]);
next++;
}
}
if (now==0) {
now=next;
next=0;
res.push_back(count_l);
count_l=0;
}
}
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&N,&M);
int id,k,child;
for (int i=0; i<M; i++) {
scanf("%d%d",&id,&k);
for (int j=0; j<k; j++) {
scanf("%d",&child);
node[id].push_back(child);
}
}
level_travel(1);
for (int i=0; i<res.size(); i++) {
printf("%d",res[i]);
if (i<res.size()-1) {
printf(" ");
}else{
printf("\n");
}
}
return 0;
}