Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #include<iostream>
- queue<int>que;
- bool visited[100];
- vector<int>adj[50];
- void bfs(int src)
- {
- que.push(src);
- visited[src]=true;
- cout<<src<<" ";
- while(!que.empty())
- {
- int parent=que.front();
- que.pop();
- for(int i=0;i<adj[parent].size();i++)
- {
- int child=adj[parent][i];
- if(visited[child]==0)
- {
- que.push(child);
- visited[child]=true;
- cout<<child<<" ";
- }
- }
- }
- }
- int main()
- {
- int node,edge,m,n;
- cin>>node>>edge;
- while(edge--)
- {
- cin>>m>>n;
- adj[m].push_back(n);
- adj[n].push_back(m);
- }
- bfs(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement