标签:mes 要求 for tree stream while math pat space
统计树中的每一层有多少叶子结点,要求逐层输出
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
using namespace std;
int N, M;
vector<int> tree[110];
vector<int> ans;
void bfs(int st)
{
queue<int> q;
q.push(st);
while(!q.empty())
{
int size = q.size();
int count = 0;
for(int i=0;i<size;i++)
{
auto cur = q.front();
q.pop();
if(tree[cur].size() == 0) //叶子结点
count++;
for(int j=0;j<tree[cur].size();j++)
q.push(tree[cur][j]);
}
ans.emplace_back(count);
}
}
int main()
{
cin >> N >> M;
int id, t, cnt;
for(int i=0;i<M;i++)
{
cin >> id;
cin >> cnt;
for(int j=0;j<cnt;j++)
{
cin >> t;
tree[id].emplace_back(t);
}
}
bfs(1);
for(int i=0;i<ans.size();i++)
i == ans.size() - 1 ? cout << ans[i]: cout << ans[i] << " ";
return 0;
}
PAT(Advanced Level)A1004. Counting Leaves
标签:mes 要求 for tree stream while math pat space
原文地址:https://www.cnblogs.com/MartinLwx/p/13773145.html