Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- map <string, vector<string> > grafo;
- vector<string> resp;
- int cases, num;
- void bfs(string no){
- queue<pair<string, int> > q;
- map<string, bool> vis;
- vis[no] = true;
- for(int i = 0; i < grafo[no].size(); i++) q.push(make_pair(grafo[no][i], 1));
- while (!q.empty()){
- pair<string, int> top = q.front();
- q.pop();
- vis[top.first] = true;
- if(top.second <= num) resp.push_back(top.first);
- for(int i = 0; i <= grafo[top.first].size(); i++){
- if(!vis[grafo[top.first][i]]){
- vis[grafo[top.first][i]] = true;
- q.push(make_pair(grafo[top.first][i], top.second + 1));
- }
- }
- }
- }
- int main(){
- cin >> cases >> num;
- string nome, amigo;
- for(int i = 0; i < cases; i++){
- cin >> nome >> amigo;
- grafo[nome].push_back(amigo);
- grafo[amigo].push_back(nome);
- }
- bfs("Rerisson");
- cout << resp.size() << endl;
- for(int i = 0; i < resp.size(); i++) cout << resp[i] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement