-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10009 bfs and pathprint.cpp
83 lines (81 loc) · 1.99 KB
/
10009 bfs and pathprint.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
#include<bits/stdc++.h>
#define N 200005
#define ll long long
#define pare pair<int,int>
using namespace std;
int res,ct,cases,n,m;
///dir array for Queen int dxq[10] = {-1,-1,-1,1,1,1,0,0};
///dir array for Queen int dyq[10] = {-1,0,1,-1,0,1,-1,1};
///dir array for knight int dxk[10] = {-2,-2,-1,-1,1,1,2,2};
///dir array for knight int dyk[10] = {1,-1,-2,2,-2,2,-1,1};
map<string,int>M;
map<int,string>U;
queue<int>Q;
vector<int>G[40005];
int path[40005];
int visited[N];
void bfs(int node){
visited[node] = 1;
Q.push(node);
while(!Q.empty()){
int u = Q.front();
Q.pop();
int i,j,k;
for(i=0;i<G[u].size();i++){
int v = G[u][i];
if(visited[v] == 0){
visited[v] = 1;
path[v] = u;
Q.push(v);
}
}
}
}
void print(string a,string b){
if(a == b){
cout<<a[0];
return;
}
print(U[path[M[a]]],b);
cout<<a[0];
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d",&cases);
while(cases--){
scanf("%d %d",&m,&n);
int i,j,k=0;
string a,b;
for(i=0;i<m;i++){
cin>>a>>b;
if(M[a] == 0){
k++;
M[a] = k;
U[k] = a;
}
if(M[b] == 0){
k++;
M[b] = k;
U[k] = b;
}
G[M[a]].push_back(M[b]);
G[M[b]].push_back(M[a]);
}
for(i=0;i<n;i++){
memset(visited,0,sizeof visited);
memset(path,0,sizeof path);
cin>>a>>b;
bfs(M[a]);
print(b,a);
printf("\n");
}
for(i=0;i<=k;i++){
G[i].clear();
}
M.clear();
U.clear();
Q = queue<int>();
if(cases)printf("\n");
}
}