https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048
很传统的树的遍历。
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m,dist[N];
vector<int>ve[N];
void bfs(int u)
{
queue<pair<int,int>>q; q.push({
1,1});
while(q.size())
{
auto t=q.front(); q.pop();
int u=t.second,len=t.first;
dist[len]++;
for(int i=0;i<ve[u].size();i++) q.push({
len+1,ve[u][i]});
}
}
int main(void)
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,k; cin>>a>>k;
for(int j=0;j<k;j++)
{
int x; cin>>x;
ve[a].push_back(x);
}
}
bfs(1);
int maxv=0,deep=0;
for(int i=1;i<=n;i++) if(dist[i]>maxv) maxv=dist[i],deep=i;
cout<<maxv<<" "<<deep;
return 0;
}