fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> adj[100001];
  4. bool visited[100001];
  5. int ans[100001];
  6. void bfs()
  7. {
  8. queue<pair<int,int> > q;
  9. //pair<int,int> p = make_pair(1,1);
  10. ans[1] = 1;
  11. q.push({1,1});
  12. while(!q.empty())
  13. {
  14. pair<int,int> p = q.front();
  15. q.pop();
  16. visited[p.first] = true;
  17. ans[p.first] = p.second;
  18. for(int i=0;i<adj[p.first].size();i++)
  19. {
  20. p = make_pair(adj[p.first][i],p.first);
  21. if(visited[p.first] == false)
  22. q.push(p);
  23. }
  24. }
  25. }
  26. int main()
  27. {
  28. memset(visited,false,sizeof(visited));
  29. memset(ans,0,sizeof(ans));
  30. int nodes,edges;
  31. cin >> nodes >> edges;
  32. for(int i=0;i<edges;i++)
  33. {
  34. int u,v;
  35. cin >> u >> v;
  36. adj[u].push_back(v);
  37. adj[v].push_back(u);
  38. }
  39. bfs();
  40. cout << "Yes\n";
  41. for(int i=2;i<=nodes;i++)
  42. cout << ans[i] << "\n";
  43. }
  44.  
Success #stdin #stdout 0s 6392KB
stdin
4 4
1 2
2 3
3 4
4 2
stdout
Yes
1
2
0