AcWing 1584. 最大的一代(BFS)
原题链接
简单
作者:
Soruto
,
2021-03-05 15:10:51
,
所有人可见
,
阅读 464
bfs(类似与z字形遍历二叉树那道题的方法)
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 110;
int n, m;
bool g[N][N];
int max_cnt = 1, max_layer = 1;
int q[N];
void bfs(int root){
int hh = 0, tt = 0, layer = 1;
q[0] = root;
while(hh <= tt){
int head = hh, tail = tt, sum = 0;
while(hh <= tail){
int t = q[hh ++];
for(int i = 1;i <= n;i ++){
if(g[t][i]) q[++ tt] = i, sum ++;
}
}
layer ++;
if(max_cnt < sum){
max_cnt = sum;
max_layer = layer;
}
}
}
int main (){
cin >> n >> m;
while(m --){
int id, k;
cin >> id >> k;
while(k --){
int son;
cin >> son;
g[id][son] = true;
}
}
bfs(1);
cout << max_cnt << " " << max_layer << endl;
return 0;
}