fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void Solve(){
  6. int n, m;
  7. cin>>n>>m;
  8. vector<int> g[n + 1];
  9. while(m--){
  10. int u, v;
  11. cin>>u>>v;
  12. g[u].push_back(v);
  13. g[v].push_back(u);
  14. }
  15. vector<bool> vst(n + 1);
  16. queue<int> q;
  17. vector<int> res(n + 1);
  18. q.push(1);
  19. vst[1] = true;
  20. while(!q.empty()){
  21. int current = q.front();
  22. q.pop();
  23. for(auto x: g[current]){
  24. if(!vst[x]){
  25. vst[x] = true;
  26. q.push(x);
  27. res[x] = current;
  28. }
  29. }
  30. }
  31. cout<<"Yes\n";
  32. for(int i = 2; i <= n; i++) cout<<res[i]<<'\n';
  33. }
  34. int main(){
  35. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  36. int T = 1;
  37. //cin>>T;
  38. while(T--){
  39. Solve();
  40. }
  41. }
Runtime error #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
Standard output is empty