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 ...
随机推荐
- 初识Pyhon之准备环境
安装成功python的运行环境之后,你可能要迫不及待大展身手了 如果你有一定的语言基础,那么基础这一块儿就可以简单的看看就可以了,但是你是一个编程语言的初学者.不着急,慢慢往下看 打开pycharm创 ...
- (转)UITextField
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- 与LCD_BPP相关的函数
board/freescale/mx6q_sabresd/mx6q_sabresd.c: panel_info.vl_bpix = LCD_BPP; common/lcd.c: off = ...
- cocos2d心得关于精灵帧缓存
在cocos2d中,精灵帧缓存CCSpriteFrameCache是用来存储精灵帧的.它没有特别的属性,只存储了一些用来管理CCSpriteFrame的方法. 以一个例子来说明,一般在又纹理图集的程序 ...
- POJ:3461-Oulipo(KMP模板题)
原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...
- Java面试——String、StringBuider以及StringBuffer的区别和使用场景
1. String.StringBuider.StringBuffer的区别 String是不可变的对象,因此在每次对String类型进行改变的时候,都会生成一个新的String对象,然后将指针指 ...
- saltstack管理八之常用执行模块
所有执行模块: http://docs.saltstack.cn/zh_CN/latest/ref/states/all/index.html 常用模块:cmd, cron, file, mount, ...
- pytorch中的math operation: torch.bmm()
torch.bmm(batch1, batch2, out=None) → Tensor Performs a batch matrix-matrix product of matrices stor ...
- Flutter 发布APK时,release版本和debug版本的默认权限不同
Flutter 发布APK时,release版本和debug版本的默认权限不同 @author ixenos 在调试模式下,默认情况下启用服务扩展和多个权限(在flutter中) 当您处于发布模式时, ...
- iOS视图边框的简单做法
我们绘制UI界面的时候,一般我们做边框是用layer,然后再给它上面添加阴影什么的,我比较喜欢用下面这个方法, UI弄几张边框的图片,用代码给图片拉伸 - (UIImage *)changeBorde ...