标签:vector max lse system 树的遍历 scanf com cto struct
#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 100010; struct node{ //bool leave;//是否是叶节点 vector<int> child; }Node[maxn]; void BFS(int root){ if(Node[root].child.size() == 0){ printf("1\n"); return; }else{ printf("0"); } queue<int> que; int size = Node[root].child.size(); for(int i=0;i<size;++i){ que.push(Node[root].child[i]); } while(!que.empty()){ int num = 0;//每一层的叶节点数 int s = que.size(); for(int i=0;i<s;++i){ int first = que.front(); que.pop(); if(Node[first].child.size() == 0){ num++; }else{ int len = Node[first].child.size(); for(int j =0;j<len;++j){ que.push(Node[first].child[j]); } } } printf(" %d",num); } } int main(){ /*for(int i=0;i<maxn;++i){ Node[i].leave = true; }*/ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<m;++i){ int root,number,children; scanf("%d%d",&root,&number); //Node[root].leave = false;//为非叶节点 for(int j=0;j<number;++j){ int t; scanf("%d",&t); Node[root].child.push_back(t); } } BFS(1); system("pause"); return 0; }
树的遍历——A1004.Counting Leaves(30) 给出一棵树,问每一层有多少叶子节点(可DFS也可BFS)
标签:vector max lse system 树的遍历 scanf com cto struct
原文地址:https://www.cnblogs.com/JasonPeng1/p/12237104.html