fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7.  
  8. int m;
  9. cin >> m;
  10.  
  11. vector<int> Graph[n+5];
  12.  
  13. for(int i=0; i<m; i++) {
  14. int x, y;
  15.  
  16. cin >> x >> y;
  17.  
  18. Graph[x].push_back(y);
  19. Graph[y].push_back(x);
  20. }
  21.  
  22. int src = 1;
  23. int vis[n+5] = {0};
  24. queue<int> q;
  25.  
  26. q.push(src);
  27. vis[src] = 1;
  28.  
  29. while(!q.empty()) {
  30. int removed = q.front();
  31.  
  32. cout << removed << endl;
  33.  
  34. q.pop();
  35.  
  36. for(auto u : Graph[removed]) {
  37. if(vis[u] == 0) {
  38. q.push(u);
  39. vis[u] = 1;
  40. }
  41. }
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5316KB
stdin
5 4
0 1
1 2
2 3
2 4
stdout
1
0
2
3
4