-
Notifications
You must be signed in to change notification settings - Fork 185
/
torn2pieces.cc
75 lines (71 loc) · 1.33 KB
/
torn2pieces.cc
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
74
75
// https://open.kattis.com/problems/torn2pieces
#include <bits/stdc++.h>
using namespace std;
using ii = tuple<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = unordered_map<string, int>;
int c;
vi vis, p;
vvi g;
void dfs(int u) {
vis[u] = 1;
for (int v : g[u])
if (!vis[v]) {
p[v] = u;
dfs(v);
}
}
int main() {
int n, c = 0;
string s, t;
cin >> n;
getline(cin, s);
vii e;
msi m;
vs mi;
for (int i = 0; i < n; i++) {
getline(cin, s);
stringstream in(s);
in >> t;
if (!m.count(t)) {
m[t] = c++;
mi.push_back(t);
}
int u = m[t];
while (in >> t) {
if (!m.count(t)) {
m[t] = c++;
mi.push_back(t);
}
int v = m[t];
e.push_back({u, v});
}
}
cin >> s >> t;
if (!m.count(s) || !m.count(t)) {
cout << "no route found\n";
return 0;
}
int a = m[s], b = m[t];
g = vvi(c);
for (auto [u, v] : e) {
g[u].push_back(v);
g[v].push_back(u);
}
p = vi(c, -1), vis = vi(c);
dfs(b);
if (p[a] < 0) cout << "no route found\n";
else {
vi path(1, a);
int u = a;
while (u != b) {
u = p[u];
path.push_back(u);
}
for (int i = 0; i < path.size(); i++)
cout << mi[path[i]] << " \n"[i == path.size() - 1];
}
}