-
Notifications
You must be signed in to change notification settings - Fork 0
/
kruskal.cpp
188 lines (162 loc) · 4.03 KB
/
kruskal.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#include <set>
#include <unordered_map>
using namespace std;
class DisjointSet {
public:
inline bool MergeSet(int x, int y) {
x = FindSet(x);
y = FindSet(y);
if (x != y) {
if (id_[y] < id_[x])
swap(x, y);
id_[x] += id_[y];
id_[y] = x;
largest_set_size_ = max(largest_set_size_, -id_[x]);
}
return x != y;
}
inline int FindSet(int x) const {
int id = id_.emplace(x, -1).first->second;
return id < 0 ? x : id_[x] = FindSet(id);
}
inline int Size(int x) const {
return -id_[FindSet(x)];
}
inline int LargestSetSize() const {
return largest_set_size_;
}
private:
mutable unordered_map<int, int> id_;
int largest_set_size_ = 1;
};
// These values are just for documentation purpose.
//
// const int MAX_C = 1000;
// const int MAX_NC = 100;
// const double MAX_JUNCTION_COST = 10.0;
// const double MAX_FAILURE_PROB = 0.4;
struct Road {
// from < to.
int from;
int to;
double distance;
};
inline bool operator<(const Road& a, const Road& b) {
if (a.to == b.to && a.from == b.from) return false;
if (a.distance < b.distance) {
return true;
}
if (a.distance > b.distance) {
return false;
}
if (a.to < b.to) {
return true;
}
if (a.to > b.to) {
return false;
}
return a.from < b.from;
}
struct City {
int id;
int x;
int y;
};
inline int distance2(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
return dx * dx + dy * dy;
}
template<typename S, typename T>
inline double distance(const S& a, const T& b) {
return sqrt(distance2(a.x, a.y, b.x, b.y));
}
typedef vector<vector<int>> Heatmap;
class RoadsAndJunctions {
private:
int S;
int NC; // Number of cities.
int MAX_NJ;
double J_COST;
double F_PROB;
vector<City> cities;
set<Road> sorted_roads;
public:
vector<int> buildJunctions(int s, vector<int> city_locations,
double junctionCost, double failureProbability) {
S = s;
NC = city_locations.size() / 2;
MAX_NJ = 2 * NC;
J_COST = junctionCost;
F_PROB = failureProbability;
cities.reserve(NC);
for (int i = 0; i < NC; ++i) {
City city;
city.id = i;
city.x = city_locations[2*i];
city.y = city_locations[2*i + 1];
cities.emplace_back(move(city));
}
for (int i = 0; i < NC; ++i) {
for (int j = i + 1; j < NC; ++j) {
Road road;
road.from = i;
road.to = j;
road.distance = distance(cities[i], cities[j]);
sorted_roads.emplace(move(road));
}
}
return {};
}
vector<int> buildRoads(vector<int> junctionStatus) {
const int numPointsToConnect = NC;
// Kruskal's algorithm.
DisjointSet dset;
vector<int> result;
result.reserve(2 * (numPointsToConnect - 1));
for (const Road& road : sorted_roads) {
int id1 = dset.FindSet(road.from);
int id2 = dset.FindSet(road.to);
if (id1 == id2) continue;
result.push_back(road.from);
result.push_back(road.to);
dset.MergeSet(id1, id2);
if (dset.LargestSetSize() == numPointsToConnect) break;
}
return result;
}
};
#ifdef LOCAL_ENTRY_POINT_FOR_TESTING
// -------8<------- end of solution submitted to the website -------8<-------
template<class T> void getVector(vector<T>& v) {
for (int i = 0; i < v.size(); ++i)
cin >> v[i];
}
int main() {
RoadsAndJunctions rj;
int S, C;
cin >> S >> C;
vector<int> cities(C);
getVector(cities);
double junctionCost, failureProbability;
cin >> junctionCost >> failureProbability;
vector<int> ret = rj.buildJunctions(S, cities, junctionCost, failureProbability);
cout << ret.size() << endl;
for (int i = 0; i < (int)ret.size(); ++i)
cout << ret[i] << endl;
cout.flush();
int J;
cin >> J;
vector<int> junctionStatus(J);
getVector(junctionStatus);
ret = rj.buildRoads(junctionStatus);
cout << ret.size() << endl;
for (int i = 0; i < (int)ret.size(); ++i)
cout << ret[i] << endl;
cout.flush();
}
#endif