PAT 1094. The Largest Generation (25)
来源:互联网 发布:985 211 知乎 编辑:程序博客网 时间:2024/06/01 09:20
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 (<N) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a family member, K (>0) is the number of his/her children, followed by a sequence of two-digit ID's of his/her children. For the sake of simplicity, let us fix the root ID to be 01. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.
Sample Input:23 1321 1 2301 4 03 02 04 0503 3 06 07 0806 2 12 1313 1 2108 2 15 1602 2 09 1011 2 19 2017 1 2205 1 1107 1 1409 1 1710 1 18Sample Output:
9 4
简述一下题目,从第二行开始输入的数,第一个是父亲id,紧接着的k是孩子个数,后面k个是孩子的id
现在给你一个家族的信息,让你求出同一辈的人最多有多少个,并且算出这些人是第几代
很明显的并查集。用途很广
#include<iostream>#include<queue>#include<algorithm>using namespace std;int father[1000];int n,m;int find(int x){if(x==father[x]) return x;return father[x]=find(father[x]);}int main(){cin>>n>>m;int s=m;for(int i=1;i<=n;i++) father[i]=i;while(s--){int f,k;cin>>f>>k;for(int i=0;i<k;i++){int num;cin>>num;father[num]=f;}}int sum=0;int len[1000+1]={0};int max=0,num=0;for(int i=1;i<=n;i++){int b=i;sum=0;while(b!=father[b]){b=father[b];sum++;//sum纪录的是第几代孩子,然后直接存入len数组}len[sum]++;}for(int i=0;i<=m;i++){if(max<len[i]){//找到同一辈最大的个数,纪录第几代max=len[i];num=i;}}cout<<max<<" "<<num+1;//注意sum是从0开始的,要+1return 0;}
广搜
遍历即可
#include<iostream> #include<queue> #include<vector> #include<stack> #include<algorithm>#include<cmath> #include<set>#include<map>#include<cstdio>using namespace std;typedef pair<int,int> P;int main(){int n,m;cin>>n>>m;vector<int> v[200];for(int i=0;i<m;i++){int num,len;scanf("%d%d",&num,&len);for(int j=0;j<len;j++){int fz;scanf("%d",&fz);v[num].push_back(fz);}}queue<P> que;que.push({1,1});int a[200]={0};a[1]=1;while(que.size()){P p=que.front();que.pop();int num=p.first;int len=p.second;for(int i=0;i<v[num].size();i++){int fz=v[num][i];que.push({fz,len+1});a[len+1]++;}}int maxn=0,index=0;for(int i=0;i<101;i++){if(a[i]>maxn) maxn=a[i],index=i;}cout<<maxn<<" "<<index;return 0;}
- PAT A 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- 【PAT】1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- pat-1094. The Largest Generation (25)
- PAT(A) - 1094. The Largest Generation (25)
- 1094. The Largest Generation (25) PAT甲级
- PAT 1094. The Largest Generation (25)
- PAT-A-1094. The Largest Generation (25)
- pat-a 1094. The Largest Generation (25)
- PAT A 1094. The Largest Generation (25)
- Pat(A) 1094. The Largest Generation (25)
- PAT 甲级 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation
- PAT 1094. The Largest Generation
- PAT--1094. The Largest Generation
- JAVA Date类、SimpleDateFormat抽象类、Calendar类
- PHPMailer Exploit Remote Code Exec CVE-2016-10033 Vuln
- 组合数之卢卡斯定理
- 分子式
- 直接插入排序
- PAT 1094. The Largest Generation (25)
- java读取文件数据进行一些简单处理的例子
- VLAN
- Ionic2 Scss
- php 栈操作匹配括号( )
- nodejs使用multer中间件上传混合表单提交(文件和非文件普通文本域)
- react-native-vector-icons使用(二)
- 几个Linux函数的使用
- Java读取文件内容的几种方式