1094. The Largest Generation (25)
来源:互联网 发布:免费工程造价软件 编辑:程序博客网 时间:2024/05/29 19:33
题目详情:https://www.patest.cn/contests/pat-a-practise/1094
提交情况:
提交代码:
#include <iostream>#include <vector>using namespace std;#define Max 110int n,m,level[Max],maxLevel,visit[Max]; //level[Max]记录各节点的层次,maxLevel记录最大的深度 vector<int> child[Max]; //用于存储各节点的孩子节点 int queue[Max],front,rear;void BFS( int start ){ visit[start] = 1; level[1] = 1; //把节点1的编号设置为1 queue[++rear] = start; while( front != rear ) { int index = queue[++front]; for( int i=0;i<child[index].size();++i ) { //孩子节点为child[index][i],下面有些写法会看得眼花缭乱 queue[++rear] = child[index][i]; //孩子节点入队 level[ child[index][i] ] = level[index] + 1; //得到孩子节点的层号 visit[ child[index][i] ] = 1; //置访问过的标志 if( maxLevel < level[index] + 1 ) //得到最深的层号 maxLevel = level[index]+1; } } } int main(){ cin>>n>>m; if( n != 0 && m == 0 ) { cout<<"1 1"<<endl; return 0; } for( int i=0;i<m;++i ) //处理输入 { int father,kids,num; cin>>father>>num; for( int j=0;j<num;++j ) { cin>>kids; child[father].push_back(kids); } } //BFS()前的初始化 front = rear = -1; BFS(1);// for( int i =1;i<=n;++i ) //查看level[]数组 // {// if( i == n )// cout<<level[i]<<endl;// else// cout<<level[i]<<" ";// }// cout<<"maxLevel is "<<maxLevel<<endl; int largest = 0,generation; //寻找人数最多的一代人和代数,即某一层上节点最多的节点数和层数 for( int i=1;i<=maxLevel;++i ) { int temp = 0; for( int j=1;j<=n;++j ) { if( i == level[j] ) ++temp; } if( temp > largest ) { largest = temp; generation = i; } } cout<<largest<<" "<<generation<<endl; return 0;}
一开始测试点1没有通过,加了这几行代码:
if( n != 0 && m == 0 ) { cout<<"1 1"<<endl; return 0; }
才对的,这代表着有n个孤立的节点,所以节点最多的某一层也就有1个节点,至于为什么是第一层的节点,我也不知道啊,就试试了就过了[不知道发生了什么的表情],个人猜测应该是从最小的层数开始的吧,应该是这样。欢迎各位大神指点啊!
0 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)
- 奇葩设置能加快Android studio的运行效率
- Spring Boot配置数据库链接池
- Frame、JPanel、JApplet的布局管理器是什么
- 方法重写与super限定
- java对象和类
- 1094. The Largest Generation (25)
- 数组类型 初始化
- Div+CSS两种盒子模型
- 表的主键与外键
- 02-线性结构3 Reversing Linked List (25分)
- 欢迎使用CSDN-markdown编辑器
- 判断一个串包含的字符串是否在另外个串中出现。
- js中arguments理解
- 九度oj 题目1007:奥运排序问题(但无法通过)