-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq.cpp
164 lines (143 loc) · 2.74 KB
/
q.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <algorithm>
#include <iostream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cassert>
#include <unordered_map>
using namespace std;
#define mp make_pair
#define st first
#define nd second
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
int n, idCount = 0, node_id, neighbour_id, src, dst;
string str, node_str, neighbour_str;
unordered_map<string, int> name_to_id;
unordered_map<int, string> id_to_name;
vector<int> adj_list[1050000];
bool visited[1050000];
int to[1050000];
int dfs(int cur)
{
if (cur == dst)
return true;
for (int neighbour : adj_list[cur])
{
if (!visited[neighbour])
{
visited[neighbour] = true;
if (dfs(neighbour))
{
to[cur] = neighbour;
return true;
}
}
}
return false;
}
int main()
{
memset(to, -1, sizeof to);
cin >> n;
getchar();
for (int i = 0; i < n; i++)
{
getline(cin, str);
node_str = "";
int j = 0;
for (; j < str.size(); j++)
{
if (str[j] == ' ')
{
while (j < str.size() and str[j] == ' ')
j++;
break;
}
node_str += str[j];
}
if (name_to_id.find(node_str) == name_to_id.end())
{
id_to_name[idCount] = node_str;
name_to_id[node_str] = idCount++;
}
node_id = name_to_id[node_str];
neighbour_str = "";
for (; j < str.size(); j++)
{
if (str[j] == ' ')
{
if (name_to_id.find(neighbour_str) == name_to_id.end())
{
id_to_name[idCount] = neighbour_str;
name_to_id[neighbour_str] = idCount++;
}
neighbour_id = name_to_id[neighbour_str];
adj_list[node_id].pb(neighbour_id);
adj_list[neighbour_id].pb(node_id);
neighbour_str = "";
while (j < str.size() and str[j] == ' ')
j++;
j--;
continue;
}
neighbour_str += str[j];
}
if (neighbour_str.size())
{
if (name_to_id.find(neighbour_str) == name_to_id.end())
{
id_to_name[idCount] = neighbour_str;
name_to_id[neighbour_str] = idCount++;
}
neighbour_id = name_to_id[neighbour_str];
adj_list[node_id].pb(neighbour_id);
adj_list[neighbour_id].pb(node_id);
neighbour_str = "";
}
}
cin >> str;
if (name_to_id.find(str) == name_to_id.end())
{
cout << "no route found";
return 0;
}
src = name_to_id[str];
cin >> str;
if (name_to_id.find(str) == name_to_id.end())
{
cout << "no route found";
return 0;
}
dst = name_to_id[str];
visited[src] = true;
if (dfs(src))
{
int cur = src;
while (true)
{
assert(cur != -1);
cout << id_to_name[cur];
if (cur != dst)
{
cout << ' ';
cur = to[cur];
}
else
break;
}
}
else
cout << "no route found";
cout << endl;
return 0;
}