-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10009 All Roads Lead Where.cpp
77 lines (68 loc) · 1.4 KB
/
10009 All Roads Lead Where.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
#include <bits/stdc++.h>
using namespace std;
vector <int> v[1000];
int dis[1002];
vector <pair <int, string> > vc;
#define pb push_back
void bfs (int src , int e)
{
dis[src] = 0;
queue <int> q;
q.push(src);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i =0 ; i < v[u].size(); i++)
{
int vx = v[u][i];
if(dis[vx] == -1)
{
dis[vx] = u; /// printing path
q.push(vx);
if(dis[vx] == e)
return;
}
}
}
}
int main()
{
int m , q;
int tes;
scanf("%d", &tes);
while(tes--){
scanf(" %d %d", &m ,&q);
string s1, s2;
int in = 0;
for(int i = 0; i <= 100; i++)
v[i].clear();
for(int i = 0; i < m; i++)
{
cin >> s1 >> s2;
int x, y;
x = s1[0] - 64;
y = s2[0] - 64;
v[x].pb(y);
v[y].pb(x);
}
while(q--)
{
cin >> s1 >> s2;
memset(dis , -1, sizeof dis);
int a, b;
a = s1[0] - 64;
b = s2[0] - 64;
bfs(b , a);
printf("%c",s1[0]);
while(a != b)
{
printf("%c", dis[a] + 64); /// printing path
a = dis[a];
}
printf("\n");
}
if(tes > 0)
printf("\n");
}
}