-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10009.cpp
More file actions
91 lines (87 loc) · 2.11 KB
/
10009.cpp
File metadata and controls
91 lines (87 loc) · 2.11 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf HUGE_VAL
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 50
string s;
string ss;
vector<int> edge[NN + 7];
map<string, int> mp;
int dist[NN + 7];
queue<int> q;
vector<string> city;
char a[1050];
char c[1050];
void bfs(int src, int dest) {
q = queue<int>();
q.push(src);
mem(dist, -1);
dist[src] = 0;
while (!q.empty()) {
int k = q.front();
q.pop();
for (int i = 0; i < edge[k].size(); i++) {
if (dist[edge[k][i]] == -1) {
q.push(edge[k][i]);
dist[edge[k][i]] = k;
if (edge[k][i] == dest) return;
}
}
}
return;
}
void go(int n) {
if (n < 1) return;
go(dist[n]);
printf("%c", city[n][0]);
return;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
int i, j, k, l, n, r;
int tc, t = 1;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &r);
j = 1;
city.push_back("0");
for (i = 0; i < n; i++) {
scanf("%s%s", &a, &c);
s.assign(a);
ss.assign(c);
if (mp.find(s) == mp.end()) {
mp[s] = j++;
city.push_back(s);
}
if (mp.find(ss) == mp.end()) {
mp[ss] = j++;
city.push_back(ss);
}
edge[mp[s]].push_back(mp[ss]);
edge[mp[ss]].push_back(mp[s]);
}
if (t != 1) puts("");
while (r--) {
scanf("%s%s", &a, &c);
s.assign(a);
ss.assign(c);
bfs(mp[s], mp[ss]);
go(dist[mp[ss]]);
printf("%c\n", city[mp[ss]][0]);
}
for (i = 0; i < NN; i++) edge[i].clear();
mp.clear();
city.clear();
t++;
}
return 0;
}