Maruf_Hasan

BFS

Dec 2nd, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. vector<int>adj[100];
  6. bool visited[100];
  7.  
  8. void bfs(int s)
  9. {
  10. queue<int>q;
  11. q.push(s);
  12. visited[s]=true;
  13. while(!q.empty())
  14. {
  15. int u=q.front();
  16. q.pop();
  17. cout<<u<<" ";
  18. for(int i=0;i<adj[u].size();i++)
  19. {
  20. if(visited[adj[u][i]]==false)
  21. {
  22. int v=adj[u][i];
  23. visited[v]=true;
  24. q.push(v);
  25. }
  26. }
  27. }
  28. cout<<endl;
  29. }
  30.  
  31.  
  32. int main()
  33. {
  34. int n,e,i,x,y;
  35. scanf("%d %d",&n,&e);
  36. for(i=0;i<e;i++)
  37. {
  38. cin>>x>>y;
  39. adj[x].push_back(y);
  40. }
  41. for(i=0;i<7;i++){
  42. bfs(i);
  43. }
  44.  
  45.  
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment