-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC305.cpp
132 lines (119 loc) · 3.1 KB
/
LC305.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
//
// Created by hf46p on 2016/5/19.
// 305. Number of Islands II
// https://leetcode.com/problems/number-of-islands-ii/
//
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
struct SimpleHash {
size_t operator()(const pair<int, int>& p) const {
return (hash<int>()(p.first) + hash<int>()(p.second));
}
};
class UnionFind {
private:
unordered_map<pair<int,int>, pair<int,int>, SimpleHash> father;
int size = 0;
public:
// UnionFind(vector<pair<int, int>>& positions){
// for (auto pos: positions) {
// father[pos] = pos;
// }
// }
UnionFind(){
}
void add(pair<int,int> a){
father[a] = a;
++ size;
}
pair<int,int> findRoot(pair<int,int> a) {
pair<int,int> i = a;
while (father[i] != i) i = father[i];
pair<int,int> root = i;
i = a;
while (i != root) {
pair<int,int> next = father[i];
father[i] = root;
i = next;
}
return root;
};
void merge(pair<int, int> a, pair<int,int> b){
pair<int, int> rootA = findRoot(a);
pair<int, int> rootB = findRoot(b);
if (rootA!=rootB){
father[rootA] = rootB;
-- size;
}
}
int countSet(){
return size;
}
};
class Solution {
private:
unordered_set<pair<int,int>, SimpleHash> S;
public:
vector<int> numIslands2(int m, int n, vector<pair<int, int>>& positions) {
UnionFind uni;
vector<int> res;
for (int i=0; i<positions.size(); ++i) {
auto pos = positions[i];
int r = pos.first;
int c = pos.second;
S.insert(positions[i]);
uni.add(positions[i]);
if (S.find(pair<int,int>(r-1, c)) != S.end()) {
uni.merge(pair<int,int>(r-1,c), pair<int,int>(r,c));
}
if (S.find(pair<int,int>(r, c+1)) != S.end()) {
uni.merge(pair<int,int>(r,c+1), pair<int,int>(r,c));
}
if (S.find(pair<int,int>(r+1, c)) != S.end()) {
uni.merge(pair<int,int>(r+1,c), pair<int,int>(r,c));
}
if (S.find(pair<int,int>(r, c-1)) != S.end()) {
uni.merge(pair<int, int>(r, c - 1), pair<int, int>(r, c));
}
res.push_back(uni.countSet());
}
return res;
}
};
//
//int main(){
// Solution sln;
// vector<pair<int, int>> p;
// p.push_back(pair<int,int>(0,0));
// p.push_back(pair<int,int>(0,1));
// p.push_back(pair<int,int>(1,2));
// p.push_back(pair<int,int>(2,1));
//
// vector<int> ans = sln.numIslands2(3,3, p);
// for (auto i : ans)
// cout << i << " ";
// cout << endl;
// return 0;
//}