-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS
66 lines (59 loc) · 1.4 KB
/
BFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
#define f(i,x,y) for (int i = x; i < y; i++)
#define fd(i,x,y) for(int i = x; i>= y; i--)
#define FOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define vint vector<int>
#define ll long long
#define clr(A,x) memset(A, x, sizeof A)
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define fst first
#define snd second
#define ones(x) __builtin_popcount(x)
#define eps (1e-9)
#define NOsync ios_base::sync_with_stdio(0)
#define oo (1<<30)
#define OO (1LL<<60)
#define N 1005
using namespace std;
vector<int> Graph[N];
int d[N];
int vis[N];
void BFS(int source){
queue<int> Q;
d[source] = 0;
vis[source] = 1;
Q.push(source);
while(!Q.empty()){
int u = Q.front();
Q.pop();
FOR(it,Graph[u]){
int v=*it;
if(!vis[v]){
vis[v]=1;
d[v] = d[u]+1;
Q.push(v);
cout<<v<<" "; // Imprime el recorrido del BFS
}
}
}
}
int main()
{
int inicio,E,a,b;
cout<<"Ingresar la cantidad de Aristas: ";
cin>>E;
cout<<"Ingresar los nodos conectados: \n";
f(i,0,E)
{
cin>>a>>b;
Graph[a].pb(b); // Nodo a apunta al Nodo b
Graph[b].pb(a); // Nodo b apunta al Nodo a
}
cout<<"\nIngresar el inicio: ";
cin>>inicio;
cout<<inicio<<" ";
BFS(inicio);
cout<<endl;
}