website/content.en/ChapterFour/0900~0999/0928.Minimize-Malware-Spread-II.md
(This problem is the same as Minimize Malware Spread, with the differences bolded.)
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.
Some nodes initial are initially infected by malware. Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.
Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.
We will remove one node from the initial list, completely removing it and any connections from this node to any other node. Return the node that if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.
Example 1:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0
Example 2:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 1
Example 3:
Input: graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
Output: 1
Note:
1 < graph.length = graph[0].length <= 3000 <= graph[i][j] == graph[j][i] <= 1graph[i][i] = 11 <= initial.length < graph.length0 <= initial[i] < graph.length(This problem is the same as Minimize Malware Spread, with the differences shown in bold.) In a network of nodes, each node i can be directly connected to another node j only when graph[i][j] = 1. Some nodes initial are initially infected by malware. As long as two nodes are directly connected and at least one of them is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner. Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops. We can remove one node from the initial list, completely removing that node and any connections from that node to any other node. Return the node that, if removed, would minimize M(initial). If multiple nodes satisfy the condition, return the node with the smallest index.
Notes:
package leetcode
import (
"math"
"github.com/halfrost/leetcode-go/template"
)
func minMalwareSpread2(graph [][]int, initial []int) int {
if len(initial) == 0 {
return 0
}
uf, minIndex, count, countMap, malwareMap, infectMap := template.UnionFind{}, initial[0], math.MinInt64, map[int]int{}, map[int]int{}, map[int]map[int]int{}
for _, v := range initial {
malwareMap[v]++
}
uf.Init(len(graph))
for i := range graph {
for j := range graph[i] {
if i == j {
break
}
if graph[i][j] == 1 && malwareMap[i] == 0 && malwareMap[j] == 0 {
uf.Union(i, j)
}
}
}
for i := 0; i < len(graph); i++ {
countMap[uf.Find(i)]++
}
// Record the number of directly adjacent malware nodes for each set
for _, i := range initial {
for j := 0; j < len(graph); j++ {
if malwareMap[j] == 0 && graph[i][j] == 1 {
p := uf.Find(j)
if _, ok := infectMap[p]; ok {
infectMap[p][i] = i
} else {
tmp := map[int]int{}
tmp[i] = i
infectMap[p] = tmp
}
}
}
}
// Select the malware node with the smallest index
for _, v := range initial {
minIndex = min(minIndex, v)
}
for i, v := range infectMap {
// Find the ones connected to only one malware node
if len(v) == 1 {
tmp := countMap[uf.Find(i)]
keys := []int{}
for k := range v {
keys = append(keys, k)
}
if count == tmp && minIndex > keys[0] {
minIndex = keys[0]
}
if count < tmp {
minIndex = keys[0]
count = tmp
}
}
}
return minIndex
}