forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolouringbook.cpp
367 lines (307 loc) · 10.1 KB
/
colouringbook.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//Constant values to be returned
constexpr int Colinear = -1, NoIntersect = 0, Intersect = 1;
constexpr int CW = 2, CCW = 3;
constexpr int Inside = 4, Outside = 5, OnEdge = 6;
//Epsilon for all double comparisons
const double EPSILON = 0.000001;
struct point
{
long double x, y;
point(long double x_=0, long double y_=0) : x(x_), y(y_){}
// Only < operator is unusual behavior
bool operator <(const point& other) const
{
return (x < other.x ? true : (x == other.x && y < other.y));
}
bool operator == (const point& other) const
{
return abs(other.x - x) < EPSILON && abs(other.y - y) < EPSILON;
}
//Add other operators as needed
point operator + (const point& other) const
{
return point(this->x + other.x, this->y + other.y);
}
point operator - (const point& other) const
{
return point(this->x - other.x, this->y - other.y);
}
point operator * (long double other) const
{
return point(this->x * other, this->y * other);
}
point operator / (long double other) const
{
return point(this->x / other, this->y / other);
}
};
//Container for line segment
struct segment { point p1, p2; };
//Dot product ab.bc
double dot(const point& a, const point& b, const point& c)
{
point AB = b - a;
point BC = c - b;
return AB.x*BC.x + AB.y*BC.y;
}
//Cross product
//AB X AC
double cross(const point& A, const point& B, const point& C)
{
point AB = B - A, AC = C - A;
return(AB.x * AC.y - AB.y * AC.x);
}
//Finds orientation of triplet of points p, q, r
//Returns Colinear, CW, or CCW
int orientation(const point& p, const point& q, const point& r)
{
double val = cross(p, q, r);
if(abs(val) < EPSILON) return Colinear;
return (val > 0) ? CW : CCW;
}
//Checks if point p is possibly on the segment s
//but doesn't guarantee it is
bool onSegment(const point& p, const segment& s)
{
bool x = (abs(s.p1.x - s.p2.x) < EPSILON && abs(p.x - s.p2.x) < EPSILON) || (p.x <= max(s.p1.x, s.p2.x) && p.x >= min(s.p1.x, s.p2.x));
bool y = (abs(s.p1.y - s.p2.y) < EPSILON && abs(p.y - s.p2.y) < EPSILON) || (p.y <= max(s.p1.y, s.p2.y) && p.y >= min(s.p1.y, s.p2.y));
return x && y;
}
//Returns of list of intersection points between segments s1, and s2
//If they do not intersect, the result is an empty vector
//If they intersect at exactly 1 point, the result contains that point
//If they overlap for non-0 distance, the left and right points of that intersection
// are returned
vector<point> intersect(const segment& s1, const segment& s2)
{
/*
cout << "Intersect:" << endl;
cout << s1.p1 << " -> " << s1.p2 << endl;
cout << s2.p1 << " -> " << s2.p2 << endl;
*/
point a = s1.p1, b = s1.p2, c = s2.p1, d = s2.p2;
if(orientation(a, b, c) == Colinear && orientation(a, b, d) == Colinear &&
orientation(c, d, a) == Colinear && orientation(c, d, b) == Colinear)
{
point min_s1 = min(a, b), max_s1 = max(a, b);
point min_s2 = min(c, d), max_s2 = max(c, d);
/*
cout << "Colinear" << endl;
cout << min_s1 << " -> " << max_s1 << endl;
cout << min_s2 << " -> " << max_s2 << endl;
*/
if(max_s1 < min_s2 || max_s2 < min_s1) return {};
point start = max(min_s1, min_s2), end = min(max_s1, max_s2);
if(start == end)
return {start};
else
return {min(start, end), max(start, end)};
}
long double a1 = b.y - a.y, a2 = d.y - c.y;
long double b1 = a.x - b.x, b2 = c.x - d.x;
long double c1 = a1*a.x + b1*a.y, c2 = a2*c.x + b2*c.y;
long double det = a1*b2 - a2*b1;
if(abs(det) > EPSILON)
{
point inter((b2*c1 - b1*c2)/det, (a1*c2 - a2*c1)/det);
//cout << "Checking " << inter << " vs segments" << endl;
//cout << onSegment(inter, s1) << " " << onSegment(inter, s2) << endl;
if(onSegment(inter, s1) && onSegment(inter, s2))
return {inter};
}
return {};
}
//Squared magnitude of point vector
double sqmag(const point& p1)
{
return p1.x*p1.x + p1.y*p1.y;
}
//Magnitude of point vector
double mag(const point& p1)
{
return sqrt(sqmag(p1));
}
//Scalar projection of vector a onto vector b
double sproject(const point& a, const point& b)
{
return dot(a, point(0, 0), b)/mag(b);
}
//Vector projection of vector a onto vector b
point vproject(const point& a, const point& b)
{
return b * sproject(a, b) / mag(b);
}
//Checks if two segments straddle each other
bool straddle(const segment& s1, const segment& s2)
{
long double cross1 = cross(s1.p1, s1.p2, s2.p1);
long double cross2 = cross(s1.p1, s1.p2, s2.p2);
if((cross1 > 0 && cross2 > 0) ||
(cross1 < 0 && cross2 < 0)) return false;
if(abs(cross1) < EPSILON && abs(cross2) < EPSILON &&
orientation(s1.p2, s2.p1, s2.p2) != Colinear)
return false;
return true;
}
//Returns distance from line (or segment) to point
long double linePointDist(const segment& s, const point& p, bool isSegment=false)
{
if(s.p1 == s.p2)
{
if(p == s.p1) return 0;
return mag(p - s.p1);
}
if(isSegment)
{
if(dot(s.p1, s.p2, p) > 0) return mag(s.p2 - p);
if(dot(s.p2, s.p1, p) > 0) return mag(s.p1 - p);
}
return abs(cross(s.p1, s.p2, p) / mag(s.p1 - s.p2));
}
//Returns positive area if points are counterclockwise,
//negative area if clockwise
long double polyArea(const vector<point>& points)
{
long double result = 0;
for(int i=0, j=1; i<points.size(); i++, j=(j+1)%points.size())
{
result += points[i].x * points[j].y;
result -= points[i].y * points[j].x;
}
return result/2;
}
//Checks if point p is inside the polygon
//Returns Inside, Outside, or OnEdge
int pointInPoly(const vector<point>& poly, const point& p)
{
//cout << "Point in poly " << p.x << " " << p.y << endl;
bool inside = false;
long double maxX = numeric_limits<long double>::lowest();
for(const point& p : poly)
maxX = max(maxX, p.x);
//Create point definitely outside polygon
point outside(maxX+1, p.y);
vector<point> intersection;
for(int i=0, j = poly.size()-1; i < poly.size(); i++, j=i-1)
{
if(p == poly[i] || p == poly[j]) return OnEdge;
if(orientation(p, poly[i], poly[j]) == Colinear &&
onSegment(p, segment{poly[i], poly[j]})) return OnEdge;
intersection = intersect(segment{p, outside}, segment{poly[i], poly[j]});
//cout << intersection.size() << " intersections with " << poly[i].x << ", " << poly[i].y << " -> " << poly[j].x << ", " << poly[j].y << endl;
if(intersection.size() == 1)
{
if(poly[i] == intersection[0] && poly[j].y <= p.y) continue;
if(poly[j] == intersection[0] && poly[i].y <= p.y) continue;
//cout << intersection[0].x << " " << intersection[0].y << endl;
inside = !inside;
}
}
//cout << "Is inside? " << inside << endl;
return (inside ? Inside : Outside);
}
//Computes the convex hull of a set of points
//Using the graham scan algorithm
vector<point> convexHull(vector<point> points) {
if(points.size() < 4) return points;
point lowestPoint = points[0];
//Don't just use point < operator because that checks
//x then y; we need y then x
for(int i=0; i<points.size(); i++)
if(points[i].y < lowestPoint.y ||
(abs(points[i].y - lowestPoint.y) < EPSILON && points[i].x < lowestPoint.x))
lowestPoint = points[i];
point horiz = lowestPoint + point(1, 0);
sort(points.begin(), points.end(),
[=](const point& l, const point& r) {
if(r == lowestPoint) return false;
if(l == lowestPoint) return true;
long double scoreL = dot(horiz, lowestPoint, l) / (mag(horiz-lowestPoint)*mag(l-lowestPoint));
long double scoreR = dot(horiz, lowestPoint, r) / (mag(horiz-lowestPoint)*mag(r-lowestPoint));
return scoreL < scoreR;
});
/*
cerr << "Hull sort" << endl;
for(point& p : points)
cerr << p << " ";
cerr << endl;
*/
points.insert(points.begin(), points.back());
uint32_t m = 1;
for(int i=2; i<points.size(); i++) {
while(cross(points[m-1], points[m], points[i]) <= -EPSILON) {
if(m > 1) { m--; continue; }
else if(i == points.size()-1) break;
else i++;
}
m++;
std::swap(points[m], points[i]);
}
points.resize(m);
if(points.front() == points.back()) points.pop_back();
return points;
}
int find(vector<int>& d, int a) {
if(d[a] == -1) return a;
return d[a] = find(d,d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d,a);
b = find(d,b);
if(a == b) return;
d[a] = b;
}
int main() {
int n, m;
cin >> n >> m;
vector<point> points(n);
for(auto& i : points) {
cin >> i.x >> i.y;
}
vector<pair<int,int>> edges(m);
for(auto& i : edges) {
cin >> i.first >> i.second;
}
vector<int> d(n,-1);
for(auto i : edges) {
join(d,i.first,i.second);
}
int cc = 0;
for(auto i : d) {
if(i == -1) cc++;
}
if(cc > 1) {
cout << -1 << endl;
return 0;
}
for(auto i : edges) {
for(auto j : edges) {
if(i == j) continue;
if(i.first == j.first) continue;
if(i.first == j.second) continue;
if(i.second == j.first) continue;
if(i.second == j.second) continue;
segment s1 = {points[i.first],points[i.second]};;
segment s2 = {points[j.first],points[j.second]};;
if(intersect(s1,s2).size() > 0) {
cout << -1 << endl;
return 0;
}
}
}
for(int j = 0; j < points.size(); j++) {
for(auto i : edges) {
if(j == i.first || j == i.second) continue;
segment s1 = {points[i.first],points[i.second]};;
segment s2 = {points[j],points[j]};
if(intersect(s1,s2).size() > 0) {
cout << -1 << endl;
return 0;
}
}
}
cout << edges.size() - points.size() + 2 << endl;
}