fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. vector<int>g[10];
  5. bool odw[10];
  6. int wierzch,kraw;
  7. void dfs(int x){
  8. cout<<x<<" ";
  9. odw[x]=true;
  10. for(int i=0; i<g[x].size(); i++)
  11. {
  12. if(!odw[g[x][i]])
  13. dfs(g[x][i]);
  14. }
  15. cout<<"("<<x<<") ";
  16. }
  17. int main()
  18. {
  19. cin>>wierzch>>kraw;
  20. for(int i=1; i<=kraw; i++)
  21. {
  22. int w1,w2;
  23. cin>>w1>>w2;
  24. g[w1].push_back(w2);
  25. g[w2].push_back(w1);
  26. }
  27. for(int i=0; i<wierzch; i++)
  28. {
  29. cout<<i<<": ";
  30. for(int j=0; j<g[i].size(); j++)
  31. {
  32. cout<<g[i][j]<<" ";
  33. }
  34. cout<<endl;
  35. }
  36. for(int i=0; i<wierzch; i++)
  37. {
  38. if(!odw[i])
  39. dfs(i);
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty