Участник:F.Nikitin/MinMalwareSpread

Материал из DISCOPAL
Перейти к: навигация, поиск
def dfs(prev, graph, visited, initial):
    visited[prev] = True
    for to in range(len(graph[prev])):
        if not graph[prev][to] or visited[to] or to == initial:
            continue
        dfs(to, graph, visited, initial)
 
class Solution(object):
    def minMalwareSpread(self, graph, initial):
        counter = [0] * len(graph)
        initial.sort()
        for init in initial:
            visited = [False] * len(graph)
            for i in initial:
                if i != init:
                    dfs(i, graph, visited, init)
            counter[init] = sum(visited)
 
        min_node = initial[0]
        for node in initial:
            if counter[node] < counter[min_node]:
                min_node = node
        return min_node