题目描述
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.
输入描述:
Each input file contains one test case. Each case starts with two positive integers N (<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (
#include<cstdio>
#include<cmath>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e2 + 10;
typedef long long LL;
vector <int> v[MAX];
int vis[MAX],o[MAX],ans,sum,cut;
void dfs(int f,int u){
if(vis[f]) return ;
vis[f] = 1; o[u]++;
for(int i = 0; i < v[f].size(); i++)
dfs(v[f][i],u + 1);
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
while(m--){
int o,u,p;
scanf("%d %d",&o,&u);
while(u--){
scanf("%d",&p);
v[o].push_back(p);
}
}
ans = sum = 0;
for(int i = 1; i <= n; i++)
if(!vis[i])
dfs(i,1);
for(int i = 1; i <= n; i++)
if(o[i] > ans) ans = o[i],sum = i;
printf("%d %d\n",ans,sum);
return 0;
}