-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.cpp
More file actions
46 lines (28 loc) · 644 Bytes
/
Copy pathBFS.cpp
File metadata and controls
46 lines (28 loc) · 644 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
int main(){
int n , q; //n = numero de nos , q = numeros de arestas
cin >> n >> q;
queue<int> D;
vector<int> A[N]; // grafo
for(int i = 0 ; i < q ; i++){
int x , y;
cin >> x >> y;
A[x].push_back(y);
A[y].push_back(x);
}
int x; // nos pelo qual a dfs começa
int vis[N]; // visitados
cin >> x;
D.push(x);
while(!D.empty()){
int a = D.front();
D.pop();
if(vis[a] == 1) continue;
vis[a] = 1;
cout << a << "\n";
for(int i = 0 ; i < A[a].size() ; i++) D.push(A[a][i]);
}
return 0;
}