fork(2) download
  1. /*
  2.   Copyright 2011 Marek "p2004a" Rusinowski
  3.   Breadth-first search
  4. */
  5. #include <cstdio>
  6. #include <vector>
  7. #include <queue>
  8.  
  9. #define MAXN 1000000
  10.  
  11. std::vector<int> edges[MAXN];
  12. bool visited[MAXN];
  13.  
  14. void bfs(int v) {
  15. std::queue<int> q;
  16. q.push(v);
  17. visited[v] = true;
  18. while (!q.empty()) {
  19. int v = q.front();
  20. q.pop();
  21. printf("%d ", v + 1);
  22. for (unsigned i = 0; i < edges[v].size(); ++i) {
  23. if (!visited[edges[v][i]]) {
  24. visited[edges[v][i]] = true;
  25. q.push(edges[v][i]);
  26. }
  27. }
  28. }
  29. }
  30.  
  31. int main() {
  32. int n, m, a, b;
  33. scanf("%d %d", &n, &m);
  34. for (int i = 0; i < m; ++i) {
  35. scanf("%d %d", &a, &b);
  36. edges[--a].push_back(--b);
  37. edges[b].push_back(a);
  38. }
  39. bfs(0);
  40. printf("\n");
  41. return 0;
  42. }
  43.  
stdin
3 2
1 2
1 3
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
1 2 3