fork download
  1. #include <bits/stdc++.h>
  2. #define maxn 1005
  3. using namespace std;
  4.  
  5. int T, v, e, u;
  6. vector<int> a[maxn];
  7. bool vis[maxn];
  8. vector<int> res;
  9. void dfs(int u){
  10. vis[u] = 1;
  11. res.push_back(u);
  12. for(int v : a[u])
  13. {
  14. if(!vis[v]){
  15. dfs(v);
  16. }
  17. }
  18. }
  19. int main()
  20. {
  21. cin >> T;
  22. while(T--){
  23. cin >> v >> e >> u;
  24. for(int i = 1; i <= v; ++i)
  25. a[i].clear();
  26. for(int i = 1; i <= e; ++i){
  27. int x, y; cin >> x >> y;
  28. a[x].push_back(y);
  29. a[y].push_back(x);
  30. }
  31. dfs(u);
  32. for(int i : res)
  33. cout << i << " ";
  34. cout << "\n";
  35. res.clear();
  36. memset(vis, false, sizeof vis);
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5284KB
stdin
1
6 9 5
1 2
1 3
2 3
2 4
3 4
3 5
4 5
4 6
5 6
stdout
5 3 1 2 4 6