-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
99 lines (88 loc) · 1.98 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class UnionFind {
public:
UnionFind(int m, int n) {
for (int i = 0; i < m * n; i++) parent.push_back(-1);
for (int i = 0; i < m * n; i++) sizes.push_back(1);
this->m = m;
this->n = n;
this->cnt = 0;
}
int index(int x, int y) {
return x * this->n + y;
}
int size(void) {
return cnt;
}
int id(int x, int y) {
if (x >= 0 && x < this->m && y >= 0 && y < this->n) {
return parent[index(x, y)];
}
return -1;
}
int add(int x, int y) {
int idx = index(x, y);
parent[idx] = idx;
sizes[idx] = 1;
cnt++;
return idx;
}
int root(int i) {
while (i != parent[i]) {
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void join(int p, int q) {
int i = root(p), j = root(q);
if (sizes[i] > sizes[j]) {
swap(i, j);
}
parent[i] = j;
sizes[j] += sizes[i];
cnt--;
}
private:
vector<int> parent, sizes;
int m, n, cnt;
};
class Solution {
public:
vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
UnionFind islands(m, n);
vector<pair<int, int>> directions = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
vector<int> ans;
for (auto& pos : positions) {
int x = pos.first, y = pos.second;
int p = islands.add(x, y);
for (auto& direction : directions) {
int q = islands.id(x + direction.first, y + direction.second);
if (q >= 0 && islands.root(p) != islands.root(q)) {
islands.join(p, q);
}
}
ans.push_back(islands.size());
}
return ans;
}
};
int main() {
int m = 3, n = 3;
vector<pair<int, int>> positions = {{0,0}, {0,1}, {1,2}, {2,1}};
Solution sol;
for (auto i: sol.numIslands2(m, n, positions)) {
cout << i << endl;
}
return 0;
}