#include <bits/stdc++.h>
using namespace std;
void bfs(vector<vector<int>> &adj, int s) {
queue<int> q;
vector<bool> visited(adj.size(), false);
visited[s] = true;
q.push(s);
while(!q.empty()){
int curr = q.front();
q.pop();
cout<<curr<<" ";
for(int x : adj[curr]){
if(!visited[x]) {
visited[x] = true;
q.push(x);
}
}
}
}
int main() {
int n = 5;
vector<vector<int>> adj(n);
adj[0].push_back(1);
adj[0].push_back(2);
adj[1].push_back(0);
adj[1].push_back(2);
adj[2].push_back(3);
adj[2].push_back(4);
adj[3].push_back(4);
adj[3].push_back(1);
adj[4].push_back(3);
adj[4].push_back(2);
bfs(adj,0);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIGJmcyh2ZWN0b3I8dmVjdG9yPGludD4+ICZhZGosIGludCBzKSB7CglxdWV1ZTxpbnQ+IHE7Cgl2ZWN0b3I8Ym9vbD4gdmlzaXRlZChhZGouc2l6ZSgpLCBmYWxzZSk7Cgl2aXNpdGVkW3NdID0gdHJ1ZTsKCXEucHVzaChzKTsKCXdoaWxlKCFxLmVtcHR5KCkpewoJCWludCBjdXJyID0gcS5mcm9udCgpOwoJCXEucG9wKCk7CgkJY291dDw8Y3Vycjw8IiAiOwoJCWZvcihpbnQgeCA6IGFkaltjdXJyXSl7CgkJCWlmKCF2aXNpdGVkW3hdKSB7CgkJCQl2aXNpdGVkW3hdID0gdHJ1ZTsKCQkJCXEucHVzaCh4KTsKCQkJfQoJCX0KCX0KfQoKaW50IG1haW4oKSB7CglpbnQgbiA9IDU7Cgl2ZWN0b3I8dmVjdG9yPGludD4+IGFkaihuKTsKCWFkalswXS5wdXNoX2JhY2soMSk7CglhZGpbMF0ucHVzaF9iYWNrKDIpOwoJYWRqWzFdLnB1c2hfYmFjaygwKTsKCWFkalsxXS5wdXNoX2JhY2soMik7CglhZGpbMl0ucHVzaF9iYWNrKDMpOwoJYWRqWzJdLnB1c2hfYmFjayg0KTsKCWFkalszXS5wdXNoX2JhY2soNCk7CglhZGpbM10ucHVzaF9iYWNrKDEpOwoJYWRqWzRdLnB1c2hfYmFjaygzKTsKCWFkals0XS5wdXNoX2JhY2soMik7CgliZnMoYWRqLDApOwoJcmV0dXJuIDA7Cn0=