PAT 1094. The Largest Generation (25)
2017-05-08 19:15
369 查看
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:
Sample Output:
9 4
简述一下题目,从第二行开始输入的数,第一个是父亲id,紧接着的k是孩子个数,后面k个是孩子的id
现在给你一个家族的信息,让你求出同一辈的人最多有多少个,并且算出这些人是第几代
很明显的并查集。用途很广
广搜
遍历即可
#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;
}
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 13 21 1 23 01 4 03 02 04 05 03 3 06 07 08 06 2 12 13 13 1 21 08 2 15 16 02 2 09 10 11 2 19 20 17 1 22 05 1 11 07 1 14 09 1 17 10 1 18
Sample 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开始的,要+1 return 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 1094. The Largest Generation (25)
- PAT (Advanced Level) 1094. The Largest Generation (25) 人数最多的一代,BFS
- PAT甲级真题及训练集(25)--1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)-PAT甲级真题(bfs,dfs,树的遍历)
- 【PAT】1094. The Largest Generation (25)
- PAT(A) - 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)(bfs遍历)
- pat-a 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 - 甲级 - 1094. The Largest Generation (25)(树的遍历DFS)
- PAT (Advanced Level) Practise 1094 The Largest Generation (25)
- PAT (Advanced Level) 1094. The Largest Generation (25)
- 【PAT】【Advanced Level】1094. The Largest Generation (25)
- PAT (Advanced Level) Practise 1094 The Largest Generation (25)