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 ...
随机推荐
- 【HDU 2126】Buy the souvenirs(01背包)
When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souve ...
- PAT Basic 1031
1031 查验身份证(15)(15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8, ...
- bash文本查看及处理工具
文本查看及处理工具: wc [OPTION] FILE... -c: 字节数 -l:行数 -w: 单词数 who | w ...
- luogu3231 [HNOI2013]消毒
前置技能:poj3041 如果是二维平面有一些方块,这些方块被染了黑色,你每次可以选择 \((x,y)\) 的区域染成白色,代价是 \(\min(x,y)\),问你付出的最小代价 显然我们不会这么染 ...
- 数据库质疑或只有MDF文件资料
--允许进行系统表的操作 use master declare @databasename varchar(255) set @databasename='Blwy BarCode' --1.如果用户 ...
- EIGRP
因为rip的收敛时间长 尤其是使用过程中 链路down掉 重收敛的时间比较长 所以在中到大型的园区网中很少用到rip协议 只有在很小的局域网中用到rip 因为收敛时间可能会稍微短一些 ...
- ES6 Arrow Function & this bug
ES6 Arrow Function & this bug let accHeadings = document.querySelectorAll(`.accordionItemHeading ...
- Codeforces 333E Summer Earnings ——Bitset
[题目分析] 找一个边长最大的三元环. 把边排序,然后依次加入.加入(i,j)时,把i和j取一个交集,看看是否存在,存在就找到了最大的三元环. 输出即可,n^3/64水过. [代码] #include ...
- 使用反射获取类中的属性(可用于动态返回PO类的列,当做表格的表头)
//利用反射取类中的属性字段 try { Class clazz = Class.forName("houji.bean.model.TaskModel"); Field[] fi ...
- hdu 1104 数论+bfs
Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...