spider68

bfs

Mar 28th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;;
  3.  
  4. vector<int> v[100];
  5. vector<int>vis(100);
  6.  
  7.  
  8. int main(){
  9.     int n,m,a,b;cin>>n>>m;
  10.     while(m--){
  11.         cin>>a>>b;
  12.         v[a].push_back(b);v[b].push_back(a);
  13.     }
  14.     queue<int> q;
  15.     q.push(1);vis[1]=true;
  16.     while(!q.empty()){
  17.         int src=q.front();q.pop();
  18.         cout<<src<<" ";
  19.         for(int i=0;i<v[src].size();i++){
  20.             if(vis[v[src][i]]==false){
  21.                q.push(v[src][i]);
  22.                vis[v[src][i]]=true;
  23.             }
  24.         }
  25.     }
  26.  
  27. }
Add Comment
Please, Sign In to add comment