Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define SZ(v) ((int)(v).size())
- #define pb push_back
- const int N = 2e5+9;
- map<string, vector<string>> adj;
- map<string, int> dist;
- void bfs () {
- queue<string> q;
- q.push("Ahmad");
- dist["Ahmad"] = 0;
- while (not q.empty()) {
- string cur = q.front();
- q.pop();
- for (auto neg : adj[cur]) {
- if (dist.count(neg) == 0) {
- dist[neg] = dist[cur] + 1;
- q.push(neg);
- }
- }
- }
- }
- void Clear () {
- adj.clear();
- dist.clear();
- }
- void run () {
- Clear();
- int n;
- cin >> n;
- for (int i = 0; i < n; ++i) {
- string a, b, c;
- cin >> a >> b >> c;
- adj[a].push_back(b);
- adj[a].push_back(c);
- adj[b].push_back(a);
- adj[b].push_back(c);
- adj[c].push_back(a);
- adj[c].push_back(b);
- }
- bfs();
- vector<pair<ll, string>> ans;
- for (auto &i : adj) {
- if (dist.count(i.first) == 0)
- ans.push_back({1e18, i.first});
- else
- ans.push_back({dist[i.first], i.first});
- }
- sort(ans.begin(), ans.end());
- cout << SZ(adj) << '\n';
- for (auto &i : ans) {
- cout << i.second << ' ';
- if (i.first == 1e18)
- cout << "undefined\n";
- else
- cout << i.first << '\n';
- }
- }
- int main () {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
- int tt;
- tt = 1;
- cin >> tt;
- while (tt--)
- run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement