class Solution:
def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
def find(x):
if x!=parents[x]:
parents[x]=find(parents[x])
return parents[x]
def union(x,y):
parents[find(x)]=find(y)
n=len(graph)
parents=[i for i in range(n)]
for i in range(len(graph)):
for j in range(i+1,len(graph)):
if graph[i][j]:
union(i,j)
area = Counter([find(i) for i in range(len(graph))])
malware = Counter([find(i) for i in initial])
# for i in initial:
# print(malware[find(i)]==1,area[find(i)],i)
return min(initial,key=lambda i: [(malware[find(i)]==1)*-area[find(i)],i])
如果当前cluster中只有一个malware node我们可以移除
只有联通的node我们才采取union