Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = 1e3;
  6. int n;
  7. vector<int> graph[maxn];
  8. int len[maxn];
  9. map<string, int> rev;
  10. map<int,string> arev;
  11. bool used[maxn];
  12. map<string, int> ans;
  13.  
  14. void bfs(int v) {
  15.     len[v] = 0;
  16.     ans[arev[v]]=0;
  17.     used[v]=true;
  18.     queue<int>q;
  19.     q.push(v);
  20.     while (!q.empty()) {
  21.         v = q.front();
  22.         q.pop();
  23.         //cout << "i'm here -> " << arev[v] << '\n';
  24.         for (int i = 0; i < graph[v].size(); ++i) {
  25.             int to = graph[v][i];
  26.             if (!used[to]) {
  27.                 used[to]=true;
  28.                 q.push(to);
  29.                 len[to] = len[v] + 1;
  30.                 ans[arev[to]]=len[v]+1;
  31.             }
  32.         }
  33.  
  34.     }
  35. }
  36.  
  37.  
  38. int main() {
  39.  
  40.     ios_base::sync_with_stdio(false);
  41.     cin.tie(nullptr);
  42.     cout.tie(nullptr);
  43.     cin >> n;
  44.     int j = 1;
  45.     for (int i = 0; i < n; ++i) {
  46.         string a, b, c;
  47.         cin >> a >> b >> c;
  48.         if(rev.find(a)==rev.end()){
  49.             rev[a]=j;
  50.             arev[j]=a;
  51.             j++;
  52.         }
  53.         if (rev.find(b)==rev.end()){
  54.             rev[b]=j;
  55.             arev[j]=b;
  56.             j++;
  57.         }
  58.         if (rev.find(c)==rev.end()){
  59.             rev[c]=j;
  60.             arev[j]=c;
  61.             j++;
  62.         }
  63.         ans[a]=-1;
  64.         ans[b]=-1;
  65.         ans[c]=-1;
  66.         graph[rev[a]].emplace_back(rev[b]);
  67.         graph[rev[a]].emplace_back(rev[c]);
  68.         graph[rev[b]].emplace_back(rev[a]);
  69.         graph[rev[b]].emplace_back(rev[c]);
  70.         graph[rev[c]].emplace_back(rev[a]);
  71.         graph[rev[c]].emplace_back(rev[b]);
  72.     }
  73.     if (rev.find("Isenbaev")!=rev.end()) {
  74.         bfs(rev["Isenbaev"]);
  75.         for (auto elem : ans) {
  76.             if (elem.second == -1) {
  77.                 cout << elem.first << " " << "undefined" << '\n';
  78.             } else {
  79.                 cout << elem.first << " " << elem.second << '\n';
  80.             }
  81.         }
  82.     }
  83.     else {
  84.         //cout << '\n';
  85.         for (auto elem : ans) {
  86.             if (elem.second == -1) {
  87.                 cout << elem.first << " " << "undefined" << '\n';
  88.             } else {
  89.                 cout << elem.first << " " << elem.second << '\n';
  90.             }
  91.         }
  92.     }
  93.     return 0;
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement