1094. The Largest Generation

来源:互联网 发布:centos 7 更改ftp目录 编辑:程序博客网 时间:2024/06/05 06:24

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.

Input Specification:

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<algorithm>#include<vector>#include<cstring>#include<queue>using namespace std;const int maxn=105;int n,m,num=1,level=1;vector<int>v[maxn];void solve(){    queue<int>q;    q.push(1);    int cnt=1,leftNum=1,curNum=0,temp,len;    while(!q.empty()){        temp=q.front();        q.pop();        leftNum--;        len=v[temp].size();        for(int i=0;i<len;i++){            q.push(v[temp][i]);            curNum++;        }        if(leftNum==0){            leftNum=curNum;            cnt++;            if(curNum>num){                num=curNum;                level=cnt;            }            curNum=0;        }    }}int main(){    //freopen("in.txt","r",stdin);    int cnt,fa,child;    scanf("%d%d",&n,&m);    for(int i=0;i<m;i++){        scanf("%d%d",&fa,&cnt);        for(int i=0;i<cnt;i++){            scanf("%d",&child);            v[fa].push_back(child);        }    }    solve();    printf("%d %d\n",num,level);    return 0;}
0 0
原创粉丝点击