You are given a network of n
nodes represented as an n x n
adjacency matrix graph
, where the ith
node is directly connected to the jth
node 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 exactly one node from initial
, 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,1],[0,1,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
Constraints:
n == graph.length
n == graph[i].length
2 <= n <= 300
graph[i][j]
is0
or1
.graph[i][j] == graph[j][i]
graph[i][i] == 1
1 <= initial.length < n
0 <= initial[i] <= n - 1
- All the integers in
initial
are unique.
class Solution:
def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
n = len(graph)
p = list(range(n))
size = [1] * n
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
clean = [True] * n
for i in initial:
clean[i] = False
for i in range(n):
if not clean[i]:
continue
for j in range(i + 1, n):
if not clean[j]:
continue
if graph[i][j] == 1:
pa, pb = find(i), find(j)
if pa == pb:
continue
p[pa] = pb
size[pb] += size[pa]
cnt = Counter()
mp = {}
for i in initial:
s = set()
for j in range(n):
if not clean[j]:
continue
if graph[i][j] == 1:
s.add(find(j))
for e in s:
cnt[e] += 1
mp[i] = s
mx = -1
res = 0
for i, s in mp.items():
t = 0
for e in s:
if cnt[e] == 1:
t += size[e]
if mx < t or (mx == t and i < res):
mx = t
res = i
return res
class Solution {
private int[] p;
public int minMalwareSpread(int[][] graph, int[] initial) {
int n = graph.length;
p = new int[n];
int[] size = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
size[i] = 1;
}
boolean[] clean = new boolean[n];
Arrays.fill(clean, true);
for (int i : initial) {
clean[i] = false;
}
for (int i = 0; i < n; ++i) {
if (!clean[i]) {
continue;
}
for (int j = i + 1; j < n; ++j) {
if (!clean[j]) {
continue;
}
if (graph[i][j] == 1) {
int pa = find(i), pb = find(j);
if (pa == pb) {
continue;
}
p[pa] = pb;
size[pb] += size[pa];
}
}
}
int[] cnt = new int[n];
Map<Integer, Set<Integer>> mp = new HashMap<>();
for (int i : initial) {
Set<Integer> s = new HashSet<>();
for (int j = 0; j < n; ++j) {
if (!clean[j]) {
continue;
}
if (graph[i][j] == 1) {
s.add(find(j));
}
}
for (int e : s) {
cnt[e] += 1;
}
mp.put(i, s);
}
int mx = -1;
int res = 0;
for (Map.Entry<Integer, Set<Integer>> entry : mp.entrySet()) {
int i = entry.getKey();
int t = 0;
for (int e : entry.getValue()) {
if (cnt[e] == 1) {
t += size[e];
}
}
if (mx < t || (mx == t && i < res)) {
mx = t;
res = i;
}
}
return res;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
class Solution {
public:
vector<int> p;
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
int n = graph.size();
vector<int> size(n, 1);
for (int i = 0; i < n; ++i) p.push_back(i);
vector<bool> clean(n, true);
for (int i : initial) clean[i] = false;
for (int i = 0; i < n; ++i)
{
if (!clean[i]) continue;
for (int j = i + 1; j < n; ++j)
{
if (!clean[j]) continue;
if (graph[i][j])
{
int pa = find(i), pb = find(j);
if (pa == pb) continue;
p[pa] = pb;
size[pb] += size[pa];
}
}
}
vector<int> cnt(n, 0);
unordered_map<int, unordered_set<int>> mp;
for (int i : initial)
{
unordered_set<int> s;
for (int j = 0; j < n; ++j)
{
if (!clean[j]) continue;
if (graph[i][j]) s.insert(find(j));
}
for (int e : s) ++cnt[e];
mp[i] = s;
}
int mx = -1;
int res = 0;
for (auto item : mp)
{
int i = item.first;
int t = 0;
for (int e : item.second)
{
if (cnt[e] == 1) t += size[e];
}
if (mx < t || (mx == t && i < res))
{
mx = t;
res = i;
}
}
return res;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};
var p []int
func minMalwareSpread(graph [][]int, initial []int) int {
n := len(graph)
p = make([]int, n)
size := make([]int, n)
clean := make([]bool, n)
for i := 0; i < n; i++ {
p[i] = i
size[i] = 1
clean[i] = true
}
for _, i := range initial {
clean[i] = false
}
for i := 0; i < n; i++ {
if !clean[i] {
continue
}
for j := i + 1; j < n; j++ {
if !clean[j] {
continue
}
if graph[i][j] == 1 {
pa, pb := find(i), find(j)
if pa == pb {
continue
}
p[pa] = pb
size[pb] += size[pa]
}
}
}
cnt := make([]int, n)
mp := make(map[int]map[int]bool)
for _, i := range initial {
s := make(map[int]bool)
for j := 0; j < n; j++ {
if !clean[j] {
continue
}
if graph[i][j] == 1 {
s[find(j)] = true
}
}
for e, _ := range s {
cnt[e]++
}
mp[i] = s
}
mx, res := -1, 0
for i, s := range mp {
t := 0
for e, _ := range s {
if cnt[e] == 1 {
t += size[e]
}
}
if mx < t || (mx == t && i < res) {
mx, res = t, i
}
}
return res
}
func find(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}