当前位置:网站首页>图的广度优先遍历
图的广度优先遍历
2022-07-16 15:05:00 【一只829】
#include<iostream>
using namespace std;
int main() {
int i, j, n, m, e[101][101], book[101] = { 0 },a,b,cur;
int que[10001], head, tail;
cin >> n >> m;
for (i = 1;i <= n;i++)
for (j = 1;j <= n;j++)
if (i == j)
e[i][j] = 0;
else
e[i][j] = -1;
for (i = 1;i <= m;i++)
{
cin >> a >> b;
e[a][b] = 1;
e[b][a] = 1;
}
//初始化
head = 1;
tail = 1;
que[tail] = 1;
tail++;
book[1] = 1;
while (head < tail) {
cur = que[head];//当前正在访问的顶点编号
for (i = 1;i <= n;i++) {
if (e[cur][i] == 1 && book[i] == 0) {
que[tail] = i;
book[i] = 1;
tail++;
}
//如果tail大于n,表明所有顶点都已经被访问过
if (tail > n) {
break;
}
}
head++;
}
for (i = 1;i < tail;i++)
cout << que[i]<<" ";
return 0;
}边栏推荐
猜你喜欢

UPUPWANK柚皮安装SSL证书指南
![LeetCode高频题:三个长度为N的无序数组A,B,C,求 A[i] + B[j] + C[k] = 64 的(i , j, k )的组合总数量](/img/b0/8ed026a2fab2a3e6c95be2a6a424d9.png)
LeetCode高频题:三个长度为N的无序数组A,B,C,求 A[i] + B[j] + C[k] = 64 的(i , j, k )的组合总数量

【Day3】Rnn

每日一题·剑指Offer || 041.滑动窗口的平均值(346一样)·队列

Spark高效数据分析01、idea开发环境搭建

不知道如何提高视觉语言大模型?浙大与联汇研究院提出新型多维度评测框架...

Do not know how to improve the visual language model? Zhejiang University and Lianhui Research Institute put forward a new multi-dimensional evaluation framework

一文详解图像中的无监督学习

文件的使用详解

华为设备配置射频调优
随机推荐
Talk about promise
聊一聊Promise
2022/7/13 cf训练(二分图染色+二分)
golang中的读写锁原理
Arthas的常见用法
Sweep redis distributed crawler deployment
【Ucos-III源码分析】——任务创建
[ CTF ]MISC encode
Can optical fiber converter solves the long-distance networking problem of taihe'an tx3016a fire engine
数据库操作软件推荐
特征值和特征向量的求法
Canal学习
Know JVM
Read write lock principle in golang
Learning path PHP -- post can't get the requested data
844. 比较含退格的字符串
【Ucos-III源码分析】——互斥信号量(互斥锁)
华为ensp网络之如何实现集线器和换机工作理验证实验?
02 review multithreading
问题来了!拔掉网线几秒,再插回去,原本的 TCP 连接还存在吗?