Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e57df73

Browse files
committed
feat: add solutions to lc problem: No.0924.Minimize Malware Spread
1 parent e0c580a commit e57df73

File tree

6 files changed

+648
-3
lines changed

6 files changed

+648
-3
lines changed

‎solution/0900-0999/0924.Minimize Malware Spread/README.md‎

Lines changed: 254 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,279 @@
5151
<li><code>0 &lt;= initial[i] &lt; graph.length</code></li>
5252
</ul>
5353

54-
5554
## 解法
5655

5756
<!-- 这里可写通用的实现逻辑 -->
5857

58+
并查集。
59+
60+
模板 1——朴素并查集:
61+
62+
```python
63+
# 初始化,p存储每个点的祖宗节点
64+
p = [i for i in range(n)]
65+
# 返回x的祖宗节点
66+
def find(x):
67+
if p[x] != x:
68+
# 路径压缩
69+
p[x] = find(p[x])
70+
return p[x]
71+
# 合并a和b所在的两个集合
72+
p[find(a)] = find(b)
73+
```
74+
75+
模板 2——维护 size 的并查集:
76+
77+
```python
78+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
79+
p = [i for i in range(n)]
80+
size = [1] * n
81+
# 返回x的祖宗节点
82+
def find(x):
83+
if p[x] != x:
84+
# 路径压缩
85+
p[x] = find(p[x])
86+
return p[x]
87+
# 合并a和b所在的两个集合
88+
size[find(b)] += size[find(a)]
89+
p[find(a)] = find(b)
90+
```
91+
92+
模板 3——维护到祖宗节点距离的并查集:
93+
94+
```python
95+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
96+
p = [i for i in range(n)]
97+
d = [0] * n
98+
# 返回x的祖宗节点
99+
def find(x):
100+
if p[x] != x:
101+
t = find(p[x])
102+
d[x] += d[p[x]]
103+
p[x] = t
104+
return p[x]
105+
# 合并a和b所在的两个集合
106+
p[find(a)] = find(b)
107+
d[find(a)] = dinstance
108+
```
109+
59110
<!-- tabs:start -->
60111

61112
### **Python3**
62113

63114
<!-- 这里可写当前语言的特殊实现逻辑 -->
64115

65116
```python
66-
117+
class Solution:
118+
def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
119+
n = len(graph)
120+
p = list(range(n))
121+
size = [1] * n
122+
123+
def find(x):
124+
if p[x] != x:
125+
p[x] = find(p[x])
126+
return p[x]
127+
128+
for i in range(n):
129+
for j in range(i + 1, n):
130+
if graph[i][j] == 1:
131+
pa, pb = find(i), find(j)
132+
if pa == pb:
133+
continue
134+
p[pa] = pb
135+
size[pb] += size[pa]
136+
137+
mi = float('inf')
138+
res = initial[0]
139+
initial.sort()
140+
for i in range(len(initial)):
141+
t = 0
142+
s = set()
143+
for j in range(len(initial)):
144+
if i == j:
145+
continue
146+
if find(initial[j]) in s:
147+
continue
148+
s.add(find(initial[j]))
149+
t += size[find(initial[j])]
150+
if mi > t:
151+
mi = t
152+
res = initial[i]
153+
return res
67154
```
68155

69156
### **Java**
70157

71158
<!-- 这里可写当前语言的特殊实现逻辑 -->
72159

73160
```java
161+
class Solution {
162+
private int[] p;
163+
164+
public int minMalwareSpread(int[][] graph, int[] initial) {
165+
int n = graph.length;
166+
p = new int[n];
167+
for (int i = 0; i < n; ++i) {
168+
p[i] = i;
169+
}
170+
int[] size = new int[n];
171+
Arrays.fill(size, 1);
172+
for (int i = 0; i < n; ++i) {
173+
for (int j = i + 1; j < n; ++j) {
174+
if (graph[i][j] == 1) {
175+
int pa = find(i), pb = find(j);
176+
if (pa == pb) {
177+
continue;
178+
}
179+
p[pa] = pb;
180+
size[pb] += size[pa];
181+
}
182+
}
183+
}
184+
int mi = Integer.MAX_VALUE;
185+
int res = initial[0];
186+
Arrays.sort(initial);
187+
for (int i = 0; i < initial.length; ++i) {
188+
int t = 0;
189+
Set<Integer> s = new HashSet<>();
190+
for (int j = 0; j < initial.length; ++j) {
191+
if (i == j) {
192+
continue;
193+
}
194+
if (s.contains(find(initial[j]))) {
195+
continue;
196+
}
197+
s.add(find(initial[j]));
198+
t += size[find(initial[j])];
199+
}
200+
if (mi > t) {
201+
mi = t;
202+
res = initial[i];
203+
}
204+
}
205+
return res;
206+
}
207+
208+
private int find(int x) {
209+
if (p[x] != x) {
210+
p[x] = find(p[x]);
211+
}
212+
return p[x];
213+
}
214+
}
215+
```
216+
217+
### **C++**
218+
219+
```cpp
220+
class Solution {
221+
public:
222+
vector<int> p;
223+
224+
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
225+
int n = graph.size();
226+
p.resize(n);
227+
for (int i = 0; i < n; ++i) p[i] = i;
228+
vector<int> size(n, 1);
229+
for (int i = 0; i < n; ++i)
230+
{
231+
for (int j = i + 1; j < n; ++j)
232+
{
233+
if (graph[i][j])
234+
{
235+
int pa = find(i), pb = find(j);
236+
if (pa == pb) continue;
237+
p[pa] = pb;
238+
size[pb] += size[pa];
239+
}
240+
}
241+
}
242+
int mi = 400;
243+
int res = initial[0];
244+
sort(initial.begin(), initial.end());
245+
for (int i = 0; i < initial.size(); ++i)
246+
{
247+
int t = 0;
248+
unordered_set<int> s;
249+
for (int j = 0; j < initial.size(); ++j)
250+
{
251+
if (i == j) continue;
252+
if (s.count(find(initial[j]))) continue;
253+
s.insert(find(initial[j]));
254+
t += size[find(initial[j])];
255+
}
256+
if (mi > t)
257+
{
258+
mi = t;
259+
res = initial[i];
260+
}
261+
}
262+
return res;
263+
}
264+
265+
int find(int x) {
266+
if (p[x] != x) p[x] = find(p[x]);
267+
return p[x];
268+
}
269+
};
270+
```
74271
272+
### **Go**
273+
274+
```go
275+
var p []int
276+
277+
func minMalwareSpread(graph [][]int, initial []int) int {
278+
n := len(graph)
279+
p = make([]int, n)
280+
size := make([]int, n)
281+
for i := 0; i < n; i++ {
282+
p[i] = i
283+
size[i] = 1
284+
}
285+
for i := 0; i < n; i++ {
286+
for j := i + 1; j < n; j++ {
287+
if graph[i][j] == 1 {
288+
pa, pb := find(i), find(j)
289+
if pa == pb {
290+
continue
291+
}
292+
p[pa] = pb
293+
size[pb] += size[pa]
294+
}
295+
}
296+
}
297+
mi := 400
298+
res := initial[0]
299+
sort.Ints(initial)
300+
for i := 0; i < len(initial); i++ {
301+
t := 0
302+
s := make(map[int]bool)
303+
for j := 0; j < len(initial); j++ {
304+
if i == j {
305+
continue
306+
}
307+
if s[find(initial[j])] {
308+
continue
309+
}
310+
s[find(initial[j])] = true
311+
t += size[find(initial[j])]
312+
}
313+
if mi > t {
314+
mi = t
315+
res = initial[i]
316+
}
317+
}
318+
return res
319+
}
320+
321+
func find(x int) int {
322+
if p[x] != x {
323+
p[x] = find(p[x])
324+
}
325+
return p[x]
326+
}
75327
```
76328

77329
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /