就是很简单的树的层次遍历。
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
vector<int> child[100];
queue<int> q;
void BFS(int &max,int &maxlevel){
int p=1,level=1,end=1;
max=1,maxlevel=1;
while(!q.empty())q.pop();
q.push(p);
while(!q.empty()){
p=q.front();
q.pop();
if(!child[p].empty()){//将p的所有孩子压入队列
for(int c:child[p])q.push(c);
}
if(end==p){
++level;
if(max<q.size()){
maxlevel=level;
max=q.size();
}
if(!q.empty())end=q.back();
}
}
}
int main(void){
//freopen("in.log","r",stdin);
int m,n,max,maxlevel;
scanf("%d%d",&n,&m);
while(m--){
int id,tmpn;
scanf("%d%d",&id,&tmpn);
while(tmpn--){
int tmpid;
scanf("%d",&tmpid);
child[id].push_back(tmpid);
}
}
BFS(max,maxlevel);
printf("%d %d",max,maxlevel);
return 0;
}