Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin ("bfs.in");
  6. ofstream fout ("bfs.out");
  7.  
  8. vector <int> graf[105];
  9. queue <int> sol;
  10. int viz[105];
  11.  
  12. void bfs()
  13. {
  14. while ( sol.size() )
  15. {
  16. int nod = sol.front();
  17. fout<<nod<<" ";
  18. sol.pop();
  19. viz[nod] = 1;
  20. for ( auto x:graf[nod] )
  21. if ( viz[x] == 0 )
  22. {
  23. viz[x] = 1;
  24. sol.push(x);
  25. }
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. int n, m, x;
  32. fin>>n>>m>>x;
  33. while ( m-- )
  34. {
  35. int a, b;
  36. fin>>a>>b;
  37. graf[a].push_back(b);
  38. graf[b].push_back(a);
  39. }
  40. sol.push(x);
  41. bfs();
  42. for ( int i = 1; i <= n; ++i )
  43. {
  44. if ( viz[i] == 0 )
  45. {
  46. sol.push(i);
  47. bfs();
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement