forked from recongamer/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
38 lines (34 loc) · 628 Bytes
/
Copy pathbfs.cpp
File metadata and controls
38 lines (34 loc) · 628 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
#include <bits/stdc++.h>
using namespace std;
int main() {
const int N=1e5+2;
vector <int> adj[N];
bool vis[N];
int n,m;
cin>>n>>m;
for(int i=0;i<N;i++){
vis[i]=0;
}
int x,y;
for(int i=0;i<m;i++){
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
queue <int> q;
q.push(1);
vis[1]=true;
while(!q.empty()){
int node=q.front();
q.pop();
cout<<node<<endl;
vector <int>:: iterator it;
for(it=adj[node].begin();it!=adj[node].end();it++){
if(!vis[*it]){
vis[*it]=1;
q.push(*it);
}
}
}
return 0;
}