Advertisement
Manioc

churras c++

Nov 15th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. map <string, vector<string> > grafo;
  6. vector<string> resp;
  7. int cases, num;
  8.  
  9. void bfs(string no){
  10.     queue<pair<string, int> > q;
  11.     map<string, bool> vis;
  12.     vis[no] = true;
  13.  
  14.     for(int i = 0; i < grafo[no].size(); i++) q.push(make_pair(grafo[no][i], 1));
  15.  
  16.     while (!q.empty()){
  17.         pair<string, int> top = q.front();
  18.         q.pop();
  19.  
  20.         vis[top.first] = true;
  21.         if(top.second <= num) resp.push_back(top.first);
  22.  
  23.         for(int i = 0; i <= grafo[top.first].size(); i++){
  24.             if(!vis[grafo[top.first][i]]){
  25.                 vis[grafo[top.first][i]] = true;
  26.                 q.push(make_pair(grafo[top.first][i], top.second + 1));
  27.             }
  28.         }
  29.     }  
  30. }
  31.  
  32. int main(){
  33.     cin >> cases >> num;
  34.     string nome, amigo;
  35.     for(int i = 0; i < cases; i++){
  36.         cin >> nome >> amigo;
  37.         grafo[nome].push_back(amigo);
  38.         grafo[amigo].push_back(nome);
  39.     }
  40.     bfs("Rerisson");
  41.     cout << resp.size() << endl;
  42.     for(int i = 0; i < resp.size(); i++) cout << resp[i] << endl;
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement