-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1174.cpp
More file actions
86 lines (80 loc) · 1.85 KB
/
Copy path1174.cpp
File metadata and controls
86 lines (80 loc) · 1.85 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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define ppb pop_back
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 1000000000
#define eps 1e-9
#define NN 111
vector<int> arr;
vector<int> e[NN];
int viw1[NN], viw2[NN];
queue<int> q;
int bfs1(int src) {
mem(viw1, -1);
viw1[src] = 0;
q = queue<int>();
q.push(src);
int i, k, l;
while (q.size()) {
int u = q.front();
q.pop();
for (i = 0; i < e[u].size(); i++) {
if (viw1[e[u][i]] == -1) {
viw1[e[u][i]] = viw1[u] + 1;
q.push(e[u][i]);
}
}
}
}
int bfs2(int src) {
mem(viw2, -1);
viw2[src] = 0;
q = queue<int>();
q.push(src);
int i, k, l;
int ans = 0;
while (q.size()) {
int u = q.front();
q.pop();
ans = max(ans, viw1[u] + viw2[u]);
for (i = 0; i < e[u].size(); i++) {
if (viw2[e[u][i]] == -1) {
viw2[e[u][i]] = viw2[u] + 1;
q.push(e[u][i]);
}
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
int t = 1, tc;
cin >> tc;
int i, j, k, l, m, n;
while (tc--) {
cin >> n >> m;
for (i = 0; i <= n; i++) e[i].clear();
for (i = 0; i < m; i++) {
cin >> k >> l;
e[k].pb(l);
e[l].pb(k);
}
int src, dest;
cin >> src >> dest;
bfs1(src);
int ans = bfs2(dest);
printf("Case %d: %d\n", t++, ans);
}
return 0;
}