Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BFS Basic
//g++ 7.4.0 #include<bits/stdc++.h> using namespace std; const int N = 100000; vector<int> adj[N]; //map<int , vector<int>> adj; bool bidir = true; vector<bool> visited(N , false); void bfs(int v) { //cout<<v<<" "; visited[v] = true; queue<int> q; q.push(v); while(! q.empty()) { int node = q.front(); q.pop(); cout<<node<<" "; for( int x : adj[node]) {if( visited[x] == true) continue; q.push(x); visited[x] = true; } } } void addEdge(int x , int y) { adj[x].push_back(y); if(bidir) adj[y].push_back(x); } void printGraph(int v) { for(int i=1 ; i<=v ;i++) { cout<<"node "<<i<<" "; for(int x : adj[i]) cout<<x<<" "; cout<<endl; } } int main() { int v,e; cin>>v>>e; //create graph in adj list for(int i=0 ;i<e ;i++) { int x,y; cin>>x>>y; addEdge(x,y); } //print graph in adj list printGraph(v); bfs(1); return 0; }
run
|
edit
|
history
|
help
0
Expected types
Test 17(2021)
hack qus
LRU - Main
Inherit
matrix
palindrome
stl_sizeof
Test 8(2020)
suffle array