forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI.cpp
More file actions
152 lines (124 loc) · 4.77 KB
/
I.cpp
File metadata and controls
152 lines (124 loc) · 4.77 KB
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const double EPS = 1e-6;
inline int cmp(double a, double b) {
return (a < b - EPS) ? -1 : ((a > b + EPS) ? 1 : 0);
}
const int MN = 5011;
struct Point {
double x, y;
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
Point operator + (Point a) { return Point(x+a.x, y+a.y); }
Point operator - (Point a) { return Point(x-a.x, y-a.y); }
Point operator * (double k) { return Point(x*k, y*k); }
Point operator / (double k) { return Point(x/k, y/k); }
double operator * (Point a) { return x*a.x + y*a.y; } // dot product
double operator % (Point a) { return x*a.y - y*a.x; } // cross product
int cmp(Point q) const { if (int t = ::cmp(x,q.x)) return t; return ::cmp(y,q.y); }
#define Comp(x) bool operator x (Point q) const { return cmp(q) x 0; }
Comp(>) Comp(<) Comp(==) Comp(>=) Comp(<=) Comp(!=)
#undef Comp
Point conj() { return Point(x, -y); }
double norm() { return x*x + y*y; }
// Note: There are 2 ways for implementing len():
// 1. sqrt(norm()) --> fast, but inaccurate (produce some values that are of order X^2)
// 2. hypot(x, y) --> slow, but much more accurate
double len() { return sqrt(norm()); }
Point rotate(double alpha) {
double cosa = cos(alpha), sina = sin(alpha);
return Point(x * cosa - y * sina, x * sina + y * cosa);
}
} a[MN];
int n;
struct DSU {
int lab[MN];
void init(int n) {
REP(i,n+1) lab[i] = -1;
}
int getRoot(int u) {
if (lab[u] < 0) return u;
return lab[u] = getRoot(lab[u]);
}
bool merge(int u, int v) {
u = getRoot(u); v = getRoot(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
} dsu;
struct Circle : Point {
double r;
Circle(double x = 0, double y = 0, double r = 0) : Point(x, y), r(r) {}
Circle(Point p, double r) : Point(p), r(r) {}
bool contains(Point p) { return (*this - p).len() <= r + EPS; }
};
bool areIntersect(Circle u, Circle v) {
if (cmp((u - v).len(), u.r + v.r) > 0) return false;
if (cmp((u - v).len() + v.r, u.r) < 0) return false;
if (cmp((u - v).len() + u.r, v.r) < 0) return false;
return true;
}
vector<Point> circleIntersect(Circle u, Circle v) {
vector<Point> res;
if (!areIntersect(u, v)) return res;
double d = (u - v).len();
double alpha = acos((u.r * u.r + d*d - v.r * v.r) / 2.0 / u.r / d);
Point p1 = (v - u).rotate(alpha);
Point p2 = (v - u).rotate(-alpha);
res.push_back(p1 / p1.len() * u.r + u);
res.push_back(p2 / p2.len() * u.r + u);
return res;
}
double dist(int i, int j) {
return sqrt(
sqr(a[i].x - a[j].x) +
sqr(a[i].y - a[j].y));
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
while (cin >> n) {
FOR(i,1,n) cin >> a[i].x >> a[i].y;
dsu.init(n+1);
FOR(i,1,n) FOR(j,i+1,n)
if (dist(i,j) < 2.0 + EPS) dsu.merge(i, j);
int res = 0;
FOR(i,1,n)
if (dsu.lab[i] < 0) res = max(res, -dsu.lab[i] + 1);
FOR(i,1,n) {
int rooti = dsu.getRoot(i);
FOR(j,i+1,n) {
int rootj = dsu.getRoot(j);
if (rooti == rootj) continue;
int sum = -dsu.lab[rooti] - dsu.lab[rootj] + 1;
set<int> allRoot; allRoot.insert(rooti); allRoot.insert(rootj);
Circle ci = Circle(a[i].x, a[i].y, 2);
Circle cj = Circle(a[j].x, a[j].y, 2);
if (!areIntersect(ci, cj)) continue;
auto intersections = circleIntersect(ci, cj);
for(auto p : intersections) {
FOR(k,j+1,n) {
int rootk = dsu.getRoot(k);
if (allRoot.count(rootk)) continue;
if ((a[k] - p).len() < 2.0 + EPS) {
allRoot.insert(rootk);
sum -= dsu.lab[rootk];
}
}
}
res = max(res, sum);
}
}
cout << res << endl;
}
}