分值 : 30 难度 : 简单题 思路 : 层序遍历 评语 : 一次就过,很爽很开心。仔细点想清楚就好了。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
#include <iostream>using namespace std ;typedef struct Node{ int son[100]; int size = 0; int level = 0 ;}Nodes ;Nodes data[100] ;int main() { int N , M ; cin>> N >>M ; for(int i = 0 ; i< M ;i++) { int temp , k ; cin >> temp >> k ; data[temp].size = k ; for(int j = 0 ; j< k ;j++) { cin >> data[temp].son[j] ; } } Nodes queen[N] ; int count = 0 , cur = 0 ; data[1].level = 0 ; int old_level = 0 ; int Count_leaves = 0 ; queen[count ++ ] = data[1]; while(cur < count) { if(queen[cur].level != old_level) { cout << Count_leaves <<" "; Count_leaves =0 ; old_level = queen[cur].level ; } if(queen[cur].size == 0 ) Count_leaves ++; for(int i = 0 ; i< queen[cur].size ;i++) { int now = queen[cur].son[i] ; data[now].level = queen[cur].level +1 ; queen[count++] = data[now] ; } cur++ ; } cout<< Count_leaves<<endl ;}