1094. The Largest Generation (25)
来源:互联网 发布:知乎二战德国石油 编辑:程序博客网 时间:2024/06/05 17:18
题目链接:https://www.patest.cn/contests/pat-a-practise/1094
题目大意:家谱以树形结构给出,辈分相同的在同一层,求人数最多的那一代人。
小tips:根节点的ID为1
#include <iostream>#include <cstdio>using namespace std;int main(int argc, char const *argv[]){ int N,M; cin>>N>>M; int Parent[103];//父节点数组,下标为该人的ID ,数组值为父亲的ID Parent[1]=0;//根节点的父亲为0 int Count_g[103]={0};//存储每一层的人数,下标为层号 int crt_P,crt_Cnt,crt_C; for(int i=0;i<M;i++){ scanf("%02d %d",&crt_P,&crt_Cnt); for(int j=0;j<crt_Cnt;j++){ scanf("%02d",&crt_C); Parent[crt_C]=crt_P; } } //计算每一层的人数 for(int i=1;i<=N;i++){ //先计算每个人属于第几层,然后更新该层的人数 int tmp_cnt=1;//层号 int tmp=Parent[i]; while(tmp){//从当前节点一直遍历到根节点 tmp_cnt++;//每升一次层号加一 tmp=Parent[tmp]; } Count_g[tmp_cnt]++;//更新该层人数 } int max=0,max_c=0; for(int i=1;i<103;i++){ if(Count_g[i]>max){ max=Count_g[i]; max_c=i; } } cout<<max<<" "<<max_c<<endl; return 0;}
结果:
阅读全文
1 0
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- AI发展受影响的高危岗位
- python + webdriver
- 器——Eclipse开发JAVA中如何Debug
- datasnap回调演示
- Java并发之Semaphore
- 1094. The Largest Generation (25)
- Softmax 函数的特点和作用
- uva12563 劲歌金曲
- 嵌套循环
- hihoCoder 1407 后缀数组二·重复旋律2 && POJ 1743 Musical Theme(最长不可重叠重复子串问题)
- 百练_2676:整数的个数
- 注解是什么,为什么要使用注解?
- Codeforces Round #382 (Div. 1) 736B Taxes
- 洛谷P1091 合唱队形