PAT 1094. The Largest Generation 用数组确定父亲,向上遍历确定相互联系
来源:互联网 发布:网络品牌策划公司 编辑:程序博客网 时间:2024/06/04 19:12
题意:
给了你一个家庭族谱,告诉你每个人的儿女分别是谁。
问哪一代人人数最多,并求出这一代人的数量。
思路:
并查集的核心在于并和查,但由于N仅仅为100,且族谱成树状关系,故无需使用并查集来求解,直接暴力即可。
用father[]确定每个人的父亲,通过每次向上搜 得知 自己是第几代, 以次方法遍历即可。
由于为N,最坏结果复杂度为N^2,而N仅为100,故直接利用该方法
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<stack>#include<vector>#include<queue>#include<string>#include<map>using namespace std;#define INF 99999999#define M 700//start 20:22//end 20:52int main(){int n,m,i,j,pid,cid,cn;int pflag[200];int father[200];memset(pflag,0,sizeof(pflag));memset(father,0,sizeof(father));scanf("%d%d",&n,&m);for(i = 0;i < m; i++){scanf("%d%d",&pid, &cn);for( j = 0 ;j < cn;j++){scanf("%d",&cid);father[cid] = pid;}}int alevel[200];int level,p;memset(alevel,0,sizeof(alevel));for(i = 1; i <= n; i++){level = 1;p = i;while(father[p] != 0){p = father[p];level++;}alevel[level]++;}int maxnum = -1;int maxlevel;for(i =1 ;i<105;i++){if(alevel[i] > maxnum){maxnum = alevel[i];maxlevel = i;}}printf("%d %d\n",maxnum,maxlevel);return 0;}
阅读全文
0 0
- PAT 1094. The Largest Generation 用数组确定父亲,向上遍历确定相互联系
- PAT 1094. The Largest Generation
- PAT 1094. The Largest Generation
- PAT--1094. The Largest Generation
- 【PAT】1094. The Largest Generation
- PAT 1094. The Largest Generation (25)(bfs遍历)
- PAT-The Largest Generation
- PAT A 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation(BFS)
- 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
- pat-1094. The Largest Generation (25)
- PAT(A) - 1094. The Largest Generation (25)
- 1094. The Largest Generation (25) PAT甲级
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- [USACO2.1]三值的排序 Sorting a Three-Valued Sequence
- hdu 1864 最大报销额
- 前端通过AJAX提交数据,后台PHP处理数据实现异步操作基本流程
- [BZOJ3790]神奇项链-manachar
- PAT 1094. The Largest Generation 用数组确定父亲,向上遍历确定相互联系
- 自媒体/新媒体写作技巧
- Repeating Decimals
- Android省市区三级联动和地区定位
- TCP/IP三次握手和四次挥手
- POJ 2241 The Tower of Babylon 笔记
- Java包命名规则
- [Freemarker] freemarker内建函数
- 栈的各种操作