-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10687.cpp
93 lines (69 loc) · 2.03 KB
/
uva-10687.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
//uva 10687
//Monitoring the Amazon
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
bool DFS(vector <vector <int> > & Graph, int start);
void DFS(vector <vector <int> > & Graph, vector <bool> & visited, int start);
int main(void)
{
int n = 0;
while (true) {
cin >> n;
if (n == 0)
break;
vector <pair <int, int> > num(n);
for (int i = 0; i < n; ++i)
cin >> num[i].first >> num[i].second;
pair <int, int> start = num[0];
sort(num.begin(), num.end());
vector <vector <int> > Graph(n);
for (int i = 0; i < n; ++i){
set < pair<double, int> > dis;
for (int j = 0; j < n; ++j)
if (i != j){
double tmp = (num[i].first - num[j].first) * (num[i].first - num[j].first)
+ (num[i].second - num[j].second) * (num[i].second - num[j].second);
tmp = sqrt(tmp);
dis.insert(make_pair(tmp, j));
}
auto a = dis.begin();
Graph[i].push_back(a->second);
++a;
Graph[i].push_back(a->second);
}
int s_index = 0;
for (int i = 0; i < n; ++i)
if (num[i] == start){
s_index = i;
break;
}
bool result = DFS(Graph, s_index);
if (result)
cout << "All stations are reachable." << endl;
else
cout << "There are stations that are unreachable." << endl;
}
return 0;
}
bool DFS(vector <vector <int> > & Graph, int start)
{
int n = Graph.size();
vector <bool> visited(n, 0);
DFS(Graph, visited, start);
bool result = 1;
for (auto a : visited)
result &= a;
return result;
}
void DFS(vector <vector <int> > & Graph, vector <bool> & visited, int start)
{
visited[start] = 1;
for (auto a : Graph[start])
if (!visited[a])
DFS(Graph, visited, a);
return;
}