Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e3;
- int n;
- vector<int> graph[maxn];
- int len[maxn];
- map<string, int> rev;
- map<int,string> arev;
- bool used[maxn];
- map<string, int> ans;
- void bfs(int v) {
- len[v] = 0;
- ans[arev[v]]=0;
- used[v]=true;
- queue<int>q;
- q.push(v);
- while (!q.empty()) {
- v = q.front();
- q.pop();
- //cout << "i'm here -> " << arev[v] << '\n';
- for (int i = 0; i < graph[v].size(); ++i) {
- int to = graph[v][i];
- if (!used[to]) {
- used[to]=true;
- q.push(to);
- len[to] = len[v] + 1;
- ans[arev[to]]=len[v]+1;
- }
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cin >> n;
- int j = 1;
- for (int i = 0; i < n; ++i) {
- string a, b, c;
- cin >> a >> b >> c;
- if(rev.find(a)==rev.end()){
- rev[a]=j;
- arev[j]=a;
- j++;
- }
- if (rev.find(b)==rev.end()){
- rev[b]=j;
- arev[j]=b;
- j++;
- }
- if (rev.find(c)==rev.end()){
- rev[c]=j;
- arev[j]=c;
- j++;
- }
- ans[a]=-1;
- ans[b]=-1;
- ans[c]=-1;
- graph[rev[a]].emplace_back(rev[b]);
- graph[rev[a]].emplace_back(rev[c]);
- graph[rev[b]].emplace_back(rev[a]);
- graph[rev[b]].emplace_back(rev[c]);
- graph[rev[c]].emplace_back(rev[a]);
- graph[rev[c]].emplace_back(rev[b]);
- }
- if (rev.find("Isenbaev")!=rev.end()) {
- bfs(rev["Isenbaev"]);
- for (auto elem : ans) {
- if (elem.second == -1) {
- cout << elem.first << " " << "undefined" << '\n';
- } else {
- cout << elem.first << " " << elem.second << '\n';
- }
- }
- }
- else {
- //cout << '\n';
- for (auto elem : ans) {
- if (elem.second == -1) {
- cout << elem.first << " " << "undefined" << '\n';
- } else {
- cout << elem.first << " " << elem.second << '\n';
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement