fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void dfs(int node, vector<vector<int>>&g,vector<int>&visited)
  5. {
  6. cout<<node<<endl;
  7. visited[node]=1;
  8. for(auto u:g[node])
  9. {
  10. if(visited[u]==0)
  11. {
  12. dfs(u,g,visited);
  13. }
  14. }
  15. }
  16. int main() {
  17. // your code goes here
  18. int n,m;
  19. cin>>n>>m;
  20. vector<vector<int>>g(n+1);
  21.  
  22. vector<int>visited(n+1);
  23.  
  24. for(int i=1;i<=m;i++)
  25. {
  26. int x,y;
  27. cin>>x>>y;
  28. g[x].push_back(y);
  29. g[y].push_back(x);
  30. }
  31. dfs(1,g,visited);
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5396KB
stdin
Standard input is empty
stdout
1