-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-11463.cpp
79 lines (57 loc) · 1.18 KB
/
uva-11463.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
//uva 11463
//Commandos
#include <iostream>
#include <climits>
#include <vector>
#include <queue>
#include <set>
using namespace std;
vector <int> BFS(vector < set <int> > & Graph, int start);
int main(void)
{
int T = 0;
int tcounter = 0;
cin >> T;
while(T--) {
++tcounter;
int n, e;
cin >> n >> e;
vector < set <int> > Graph(n);
for(int i = 0; i < e; ++i){
int u, v;
cin >> u >> v;
Graph[u].insert(v);
Graph[v].insert(u);
}
int s, d;
cin >> s >> d;
vector <int> bfsStart = BFS(Graph, s);
vector <int> bfsDis = BFS(Graph, d);
int result = INT_MIN;
for(int i = 0; i < n; ++i)
result = max(result, bfsStart[i] + bfsDis[i]);
cout << "Case " << tcounter << ": " << result << endl;
}
return 0;
}
vector <int> BFS(vector < set <int> > & Graph, int start)
{
int n = Graph.size();
vector <int> levels(n, INT_MAX);
vector <bool> visited(n, false);
levels[start] = 0;
visited[start] = 1;
queue <int> Queue;
Queue.push(start);
while(!Queue.empty()){
int front = Queue.front();
Queue.pop();
for(auto a : Graph[front])
if(!visited[a]){
visited[a] = true;
levels[a] = levels[front] + 1;
Queue.push(a);
}
}
return levels;
}