-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path627.cpp
More file actions
85 lines (80 loc) · 1.91 KB
/
627.cpp
File metadata and controls
85 lines (80 loc) · 1.91 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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @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))
int dist[333];
int par[333];
vector<int> edge[333];
map<string, int> mp;
queue<int> q;
char a[55555];
int bfs(int src, int dest) {
int i, j, k;
mem(dist, -1);
q = queue<int>();
q.push(src);
dist[src] = 0;
while (!q.empty()) {
k = q.front();
q.pop();
for (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 k;
}
}
}
return -1;
}
void go(int n) {
if (n < 1) return;
go(dist[n]);
printf("%d ", n);
return;
}
int main() {
int tc, t = 1;
int n, i, j, k, l, p, r, m;
int sum = 0;
while (~scanf("%d", &tc)) {
m = tc;
while (tc--) {
scanf("%s", &a);
l = strlen(a);
for (i = 0; i <= l; i++) {
if (a[i] == '-') {
k = sum;
sum = 0;
} else if (a[i] == ',' || a[i] == 0) {
edge[k].push_back(sum);
sum = 0;
} else
sum = sum * 10 + a[i] - '0';
}
}
scanf("%d", &p);
puts("-----");
while (p--) {
scanf("%d%d", &k, &l);
n = bfs(k, l);
if (n == -1)
puts("connection impossible");
else {
go(dist[l]);
printf("%d\n", l);
}
}
for (i = 0; i <= m; i++) edge[i].clear();
}
return 0;
}