-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLightOJ 1174.cpp
102 lines (85 loc) · 1.76 KB
/
LightOJ 1174.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define INF -1
vector<int> adj_l[200];
int dist1[200], dist2[200], ans[200];
void init(){
mem(dist1, INF);
mem(dist2, INF);
mem(ans, 0);
for(int i = 0; i < 200; i++){
adj_l[i].clear();
}
}
void input(int u, int v){
adj_l[u].push_back(v);
adj_l[v].push_back(u);
}
void bfs_1(int src)
{
queue<int> q;
dist1[src] = 0;
q.push(src);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 0; i < adj_l[u].size(); i++)
{
int v = adj_l[u][i];
if(dist1[v] == INF)
{
dist1[v] = dist1[u] + 1;
q.push(v);
}
}
}
}
void bfs_2(int src)
{
queue<int> q;
dist2[src] = 0;
q.push(src);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 0; i < adj_l[u].size(); i++)
{
int v = adj_l[u][i];
if(dist2[v] == INF)
{
dist2[v] = dist2[u] + 1;
q.push(v);
}
}
}
}
int main(){
int t, u, v, V, E, s, d, case_no = 0, mx = 0;
cin>>t;
while(t--){
init();
cin>>V>>E;
for(int i = 0; i < E; i++){
cin>>u>>v;
input(u, v);
}
cin>>s>>d;
bfs_1(s);
bfs_2(d);
for(int i = 0; i < V; i++){
ans[i] = dist1[i] + dist2[i];
}
for(int i = 0; i < V; i++){
if(ans[i] > mx)
mx = ans[i];
}
printf("Case %d: %d\n", ++case_no, mx);
mx = 0;
}
return 0;
}