PAT 1094. The Largest Generation(BFS)
CODE:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; bool mat[105][105];
bool root[105];
int n,m;
int R;
int cnt[105];
int ans1,ans2; struct TNode
{
int num;
int level;
}; void BFS()
{
queue<TNode> Q;
TNode first;
first.num=R;
first.level=1;
TNode next;
Q.push(first);
while(!Q.empty())
{
first=Q.front();
cnt[first.level]++;
Q.pop();
for(int j=1;j<=n;j++)
{
if(mat[first.num][j]==true)
{
next.num=j;
next.level=first.level+1;
Q.push(next);
}
}
}
for(int i=1;i<=n;i++)
{
if(ans1<cnt[i])
{ans1=cnt[i];
ans2=i;
}
}
} int main()
{
int id,k;
while(scanf("%d%d",&n,&m)==2)
{
memset(mat,false,sizeof(mat));
memset(root,true,sizeof(root));
memset(cnt,0,sizeof(cnt));
int flag=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&id,&k);
int id1;
while(k--)
{
scanf("%d",&id1);
mat[id][id1]=true;
root[id1]=false;
}
for(int i=1;i<=n;i++)
{
if(root[i]==true)
{
for(int j=1;j<=n;j++)
{
if(mat[i][j]==true)
{
flag=1;
R=i;
break;
}
}
}
if(flag)
break;
}
}
ans1=-1;
BFS();
printf("%d %d\n",ans1,ans2);
}
return 0;
}
PAT 1094. The Largest Generation(BFS)的更多相关文章
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
- PAT 1094. The Largest Generation (层级遍历)
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- 1094 The Largest Generation ——PAT甲级真题
1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]
题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...
- PAT A1094 The Largest Generation (25 分)——树的bfs遍历
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT 甲级 1094 The Largest Generation
https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048 A family hierarchy is ...
随机推荐
- Flask 系列之 构建 Swagger UI 风格的 WebAPI
说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验 环境初始化 # 创建项目目录 mkdir helloworl ...
- 记我的小网站发现的Bug之一 —— 某用户签到了两次
1.故事背景 今天上午我忙完手中的事情之后突然想起来我还没签到,于是赶紧打开签到页面,刚点击了签到按钮,提示"签到成功,获得25阅读额度!",正准备退出浏览器,忽然发现签到列表有异 ...
- Java的9种基本数据类型以及封装类
Java的9种基本数据类型以及封装类 基本类型 大小(单位/字节) 默认值 封装类 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 0 Integer ...
- 算法学习记录-图——最小生成树之prim算法
一个连通图的生成树是一个极小的连通子图,它包含图中全部的顶点(n个顶点),但只有n-1条边. 最小生成树:构造连通网的最小代价(最小权值)生成树. prim算法在严蔚敏树上有解释,但是都是数学语言,很 ...
- 二分查找与 bisect 模块
Python 的列表(list)内部实现是一个数组,也就是一个线性表.在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n).对于大数据量,则可以用二分查找进行优化.二分查找 ...
- IOS 自动布局-UIStackPanel和UIGridPanel(五)
试想这样的一个需求场合,一个button靠右显示,并且距离superView的顶部和右边间距分别为10和5.如下图所示: 要实现这样的需求,如果不用自动布局技术,那么我们能想到的就是老老实实的使用绝对 ...
- win10关闭防火墙命令
NetSh Advfirewall set allprofiles state off #关闭防火墙 Netsh Advfirewall show allprofiles #查看防火墙状态
- 怎么样给CentOS6.5增加swap分区
再给服务器添加zabbix监控的时候,发现服务器有个报错“Lack of free swap space on localhost”,通过查找得知,在安装服务器的时候忘了划分swap分区.为了减少报错 ...
- 算法复习——费用流模板(poj2135)
题目: Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16898 Accepted: 6543 De ...
- 转载:c++打印日志文件的一个模板
转载地址:http://blog.csdn.net/huangyifei_1111/article/details/52134914 NetDataLog.h #ifndef NETDATALOG_H ...