-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
151 lines (138 loc) · 4.41 KB
/
solution.h
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
typedef pair<int, int> pii;
struct DSU {
std::vector<int> f;
DSU(int n) : f(n) { std::iota(f.begin(), f.end(), 0); }
int leader(int x) {
if (f[x] == x) {
return x;
}
int y = leader(f[x]);
f[x] = y;
return f[x];
}
bool merge(int a, int b) {
auto x = leader(a);
auto y = leader(b);
if (x == y) {
return false;
}
f[x] = y;
return true;
}
};
class Solution {
public:
int containVirus(vector<vector<int>>& v) {
int n = v.size();
int m = v[0].size();
auto get_idx = [&](int x, int y) {
return x * m + y;
};
auto get_loc = [&](int idx) -> pii {
return {idx / m, idx % m};
};
unordered_set<int> ok;
unordered_set<int> not_ok;
DSU dsu(n * m);
int d[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == 0) {
ok.insert(get_idx(i, j));
} else {
for (int k = 0; k < 4; k++) {
int i2 = i + d[k][0], j2 = j + d[k][1];
if (i2 >= 0 && i2 < n && j2 >= 0 && j2 < m && v[i2][j2]) {
dsu.merge(get_idx(i, j), get_idx(i2, j2));
}
}
not_ok.insert(get_idx(i, j));
}
}
}
int ans = 0;
while (not_ok.size() != 0 && ok.size() != 0) {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// if (not_ok.count(get_idx(i, j))) {
// cout << "1 ";
// } else {
// cout << "0 ";
// }
// }
// cout << endl;
// }
debug(ans);
unordered_map<int, int> count, use;
for (auto it : ok) {
auto [x, y] = get_loc(it);
unordered_set<int> tmp;
for (int i = 0; i < 4; i++) {
int x2 = x + d[i][0], y2 = y + d[i][1];
int idx2 = get_idx(x2, y2);
if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && not_ok.count(idx2)) {
int fidx2 = dsu.leader(idx2);
use[fidx2]++;
tmp.insert(fidx2);
}
}
for (auto it : tmp) {
count[it]++;
}
}
int fmx = dsu.leader(*not_ok.begin());
for (auto it : count) {
if (count[fmx] < it.second) {
fmx = it.first;
}
}
ans += use[fmx];
for (int i = 0; i < n * m; i++) {
if (dsu.leader(i) == fmx) {
not_ok.erase(i);
}
}
vector<int> tmp;
for (auto it : ok) {
auto [x, y] = get_loc(it);
bool flag = true;
for (int i = 0; i < 4; i++) {
int x2 = x + d[i][0], y2 = y + d[i][1];
int idx2 = get_idx(x2, y2);
if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && not_ok.count(idx2)) {
dsu.merge(it, idx2);
flag = false;
}
}
if (!flag) {
tmp.push_back(it);
}
}
for (auto it : tmp) {
ok.erase(it);
not_ok.insert(it);
}
for (auto it : not_ok) {
auto [x, y] = get_loc(it);
for (int i = 0; i < 4; i++) {
int x2 = x + d[i][0], y2 = y + d[i][1];
int idx2 = get_idx(x2, y2);
if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && not_ok.count(idx2)) {
dsu.merge(it, idx2);
}
}
}
}
return ans;
}
};