标签:clu return 代码 math namespace 最好 The ges a10
树的层序遍历的问题,找到结点数最多的一层,输出结点树和对应层号
BFS
做,可以说是裸模版的题目了#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
using namespace std;
vector<int> tree[110];
void bfs(int st, int& numbers, int& level)
{
queue<int> q;
q.push(st);
int depth = 1;
while(!q.empty())
{
int size = q.size();
if(size > numbers) //和解进行比较,找最好
{
numbers = size;
level = depth;
}
for(int i=0;i<size;i++)
{
auto cur = q.front();
q.pop();
for(int j=0;j<tree[cur].size();j++)
q.push(tree[cur][j]);
}
depth++;
}
}
int main()
{
int n, m;
cin >> n >> m;
int id, k, t;
for(int i=0;i<m;i++)
{
cin >> id >> k;
for(int j=0;j<k;j++)
{
cin >> t;
tree[id].emplace_back(t);
}
}
int level_numbers = -1, level = -1;
bfs(1, level_numbers, level);
cout << level_numbers << " " << level;
return 0;
}
PAT(Advanced Level)A1094. The Largest Generation
标签:clu return 代码 math namespace 最好 The ges a10
原文地址:https://www.cnblogs.com/MartinLwx/p/13769712.html