Advertisement
Hamoudi30

Untitled

Aug 19th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define SZ(v) ((int)(v).size())
  5. #define pb push_back
  6. const int N = 2e5+9;
  7. map<string, vector<string>> adj;
  8. map<string, int> dist;
  9. void bfs () {
  10.     queue<string> q;
  11.     q.push("Ahmad");
  12.     dist["Ahmad"] = 0;
  13.     while (not q.empty()) {
  14.         string cur = q.front();
  15.         q.pop();
  16.         for (auto neg : adj[cur]) {
  17.             if (dist.count(neg) == 0) {
  18.                 dist[neg] = dist[cur] + 1;
  19.                 q.push(neg);
  20.             }
  21.         }
  22.     }
  23. }
  24. void Clear () {
  25.     adj.clear();
  26.     dist.clear();
  27. }
  28. void run () {
  29.     Clear();
  30.     int n;
  31.     cin >> n;
  32.     for (int i = 0; i < n; ++i) {
  33.         string a, b, c;
  34.         cin >> a >> b >> c;
  35.         adj[a].push_back(b);
  36.         adj[a].push_back(c);
  37.         adj[b].push_back(a);
  38.         adj[b].push_back(c);
  39.         adj[c].push_back(a);
  40.         adj[c].push_back(b);
  41.     }
  42.     bfs();
  43.     vector<pair<ll, string>> ans;
  44.     for (auto &i : adj) {
  45.         if (dist.count(i.first) == 0)
  46.             ans.push_back({1e18, i.first});
  47.         else
  48.             ans.push_back({dist[i.first], i.first});
  49.     }
  50.     sort(ans.begin(), ans.end());
  51.     cout << SZ(adj) << '\n';
  52.     for (auto &i : ans) {
  53.         cout << i.second << ' ';
  54.         if (i.first == 1e18)
  55.             cout << "undefined\n";
  56.         else
  57.             cout << i.first << '\n';
  58.     }
  59. }
  60. int main () {
  61.     ios_base::sync_with_stdio(false);
  62.     cin.tie(nullptr);
  63.     cout.tie(nullptr);
  64. //    freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
  65.     int tt;
  66.     tt = 1;
  67.     cin >> tt;
  68.     while (tt--)
  69.         run();
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement