又是一个遍历,这次用bfs练练手~
#include<bits/stdc++.h>
using namespace std;
int l[100]={};
struct node{
int layer;
vector<int> child;
}t[100];
void bfs(int root){
queue<int> q;
q.push(root);
while(q.size()){
int now=q.front();
q.pop();
for(int i=0;i<t[now].child.size();i++){
t[t[now].child[i]].layer=t[now].layer+1;
l[t[t[now].child[i]].layer]++;
q.push(t[now].child[i]);
}
}
}
int main(){
int n,m;
cin>>n>>m;
int temp,k,a;
for(int i=1;i<=m;i++){
cin>>temp>>k;
for(int j=1;j<=k;j++){
cin>>a;
t[temp].child.push_back(a);
}
}
int root=1;
l[root]=1;
t[root].layer=1;
bfs(root);
int maxn=0;
int tag=0;
for(int i=0;i<100;i++){
if(l[i]>maxn){
tag=i;
maxn=l[i];
}
}
printf("%d %d",maxn,tag);
return 0;
}