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