利用bfs遍历
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
vector<int>child[111];
int maxp=0;
int level=0;
int people[100]={0};
queue<int>id;
//queue<int>people
queue<int>leve;
void bfs(int cur,int curlevel){
people[curlevel]++;
if(maxp<people[curlevel]) {
maxp=people[curlevel];
level=curlevel;
}
for(int i=0;i<child[cur].size();i++){
int temp=child[cur][i];
// cout<<temp<<" ";
id.push(temp);
leve.push(curlevel+1);
}
if(!id.empty()){
int nextid,nextlevel;
nextid=id.front();
nextlevel=leve.front();
id.pop();
leve.pop();
bfs(nextid,nextlevel);
}
return;
}
int main(){
int n;
cin>>n;
int m;
cin>>m;
for(int i=0;i<m;i++){
int id;
cin>>id;
int k;
cin>>k;
for(int j=0;j<k;j++){
int num;
cin>>num;
child[id].push_back(num);
}
}
// cout<<child[1].size();
bfs(1,1);
cout<<maxp<<" "<<level;
}