-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Closest_Point.cpp
76 lines (67 loc) · 1.72 KB
/
A_Closest_Point.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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
class UnionF {
public:
vec pt, size;
int cmp;
UnionF(int n) {
pt.resize(n);
size.resize(n, 1);
cmp = 0;
for (int i = 0; i < n; i++) {
pt[i] = i;
}
}
int find(int x) {
if (x != pt[x]) {
pt[x] = find(pt[x]);
}
return pt[x];
}
void us(int x, int y) {
int rx = find(x);
int ry = find(y);
if (rx != ry) {
if (size[rx] < size[ry]) {
swap(rx, ry);
}
pt[ry] = rx;
size[rx] += size[ry];
cmp--;
}
}
};
int main() {
int n, m, q;
cin >> n >> m >> q;
UnionF uf(n * m);
vector<vector<bool>> mat(n, vector<bool>(m, 0));
vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int dropCount = 0;
while (q--) {
char cmd;
cin >> cmd;
if (cmd == 'd') {
int x, y;
cin >> x >> y;
x--; y--;
int index = x * m + y;
if (!mat[x][y]) {
mat[x][y] = 1;
uf.cmp++;
for (auto &dir : directions) {
int nx = x + dir.first;
int ny = y + dir.second;
if (nx >= 0 && ny >= 0 && nx < n && ny < m && mat[nx][ny]) {
int neighborIndex = nx * m + ny;
uf.us(index, neighborIndex);
}
}
}
} else if (cmd == 'c') {
cout << uf.cmp << endl;
}
}
return 0;
}