-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.cpp
72 lines (70 loc) · 1.45 KB
/
bfs.cpp
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
67
68
69
70
71
72
#include <iostream>
#include <cmath>
#include <stack>
#include <cstring>
#include <stdlib.h>
#include <unordered_set>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <queue>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define pii vector<int, int>
#define vii vector<pii>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ff first
#define ss second
const int N=1e5+2;
bool vis[N];
vi adj[N];
int main(){
rep(i,0,N){
vis[i]=0;
}
int n=7,m=7;
rep(i,0,m){
int x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
queue<int> q;
q.push(1);
vis[1]=1;
while(!q.empty()){
int node=q.front();
q.pop();
cout<<node<<endl;
vi :: iterator it;
for(it=adj[node].begin();it!=adj[node].end();it++){
if(!vis[*it]){
vis[*it]=1;
q.push(*it);
}
}
}
return 0;
}
vector<int> bfs(int v , vi adj[]){
vector<int> vis(v,0);
vector<int> ans;
queue<int> q;
q.push(0);
vis[0]=1;
while(!q.empty()){
int node=q.front();
q.pop();
ans.push_back(node);
for(auto i:adj[node]){
if(!vis[i]){
vis[i]=1;
q.push(i);
}
}
}
return ans;
}