fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int graph[1001][1001];
  4. int visit[1001];
  5. int n, e;
  6.  
  7. void DFS(int start)
  8. {
  9. visit[start] = 1;
  10. cout<<start<<" ";
  11.  
  12. for(int j = 1; j <= n; j++)
  13. {
  14. if(visit[j] == 0 && graph[start][j] != 0)
  15. {
  16. DFS(j);
  17. }
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. cin>>n>>e;
  24. int u, v;
  25. for(int i = 1; i <= e; i++)
  26. {
  27. cin>>u>>v;
  28. graph[u][v] = 1;
  29. graph[v][u] = 1;
  30. }
  31. DFS(1);
  32. }
  33.  
Success #stdin #stdout 0s 5280KB
stdin
10 13                                                                            1 2                                                                              1 4                                                                              4 3                                                                              2 3                                                                              3 10                                                                             3 9                                                                              2 5                                                                              2 7                                                                              2 8                                                                              5 6                                                                              5 7                                                                              5 8                                                                              7 8
stdout
1 2 3 4 9 10 5 6 7 8