一开始测试点2不通过 发现时漏了N == 1 M == 0情况下的输出;
一种解决方法是特判这种情况,输出1;
更好的方法是把maxlevl初始值设为1,这样程序也会正常输出;
程序bug是靠debug发现的 所以出现问题还是得多试试debug呀!
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 110;
int n,m;
struct node{
int level;
int data;
vector<int> child;
}Node[maxn];
int maxlevel = 1;
void BFS(int root){
queue<int> q;
q.push(root);
Node[root].level = 1;
while(!q.empty()){
int newnode = q.front();
q.pop();
for(int i=0;i<Node[newnode].child.size();i++){
int child = Node[newnode].child[i];
Node[child].level = Node[newnode].level + 1;
q.push(child);
if(Node[child].level > maxlevel) maxlevel = Node[child].level;
}
}
}
int main(){
int num_leaf,leaf;
int value;
scanf("%d",&n);
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d %d",&leaf,&num_leaf);
for(int j=0;j<num_leaf;j++){
scanf("%d",&value);
Node[leaf].child.push_back(value);
}
}
/*
if (n == 1 && m == 0) {
printf("1");
return 0;
} *///对应测试点2 即当 N==0且 M==1时,应该输出1
BFS(1);
//printf("%d ",maxlevel);
//for(int i=1;i<=n;i++) printf("%d ",Node[i].level);
int arr[maxlevel+10] = {0};
for(int i = 1 ; i <= n; i++){
if(Node[i].child.size() == 0) arr[Node[i].level] += 1;
}
for(int i = 1;i <= maxlevel; i++){
printf("%d",arr[i]);
if(i != maxlevel) printf(" ");
}
return 0;
}