-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1837.cpp
More file actions
73 lines (60 loc) · 1.57 KB
/
1837.cpp
File metadata and controls
73 lines (60 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <iostream>
using namespace std;
const int N = 3007;
const int S = 27;
int n, m;
int dist[N];
vector<int> adj[N];
map<string, int> mp;
map<string, int>::iterator it;
char str[3][S];
int id[3];
queue<int> q;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> str[0] >> str[1] >> str[2];
for (int j = 0; j < 3; j++) {
if (mp.find(str[j]) == mp.end()) {
id[j] = mp.size();
mp[str[j]] = id[j];
}
id[j] = mp[str[j]];
}
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
adj[id[j]].push_back(id[k]);
}
}
}
for (int i = 0; i < mp.size(); i++)
dist[i] = -1;
if (mp.find("Isenbaev") != mp.end()) {
dist[mp["Isenbaev"]] = 0;
q.push(mp["Isenbaev"]);
}
while (!q.empty()) {
int aux = q.front();
q.pop();
if (dist[aux] == -1)
continue;
for (int i = 0; i < adj[aux].size(); i++) {
if (dist[adj[aux][i]] != -1 && dist[adj[aux][i]] <= dist[aux])
continue;
dist[adj[aux][i]] = dist[aux] + 1;
q.push(adj[aux][i]);
}
}
for (it = mp.begin(); it != mp.end(); ++it) {
//printf("%s ", it->first.c_str());
cout << it->first.c_str() << " ";
if (dist[it->second] == -1)
cout << "undefined" << endl;
else
cout << dist[it->second] << endl;
}
}