python solution with my thinking process (with extra Chinese explanation)
you can get Chinese explanation in \nhttps://buptwc.github.io/2018/10/21/Leetcode-928-Minimize-Malware-Spread-II/\n\nThe idea of this question is similar to lee
you can get Chinese explanation in \nhttps://buptwc.github.io/2018/10/21/Leetcode-928-Minimize-Malware-Spread-II/\n\nThe idea of this question is similar to [leetcode 924 Minimize Malware Spread](https://leetcode.com/problems/minimize-malware-spread/discuss/181186/python-dfs-solution-with-picture-to-explain-my-thought(with-extra-Chinese-explanation)).\n\nThe key point of these two questions is to analyze how to change a node from an infected state to a safe one.\n\n![image](https://assets.leetcode.com/users/2017111303/image_1540136419.png)\nas picture shows, the yellow node is the initial infected node.\nfor the safe node [1,2,3,5,6], we analyze one by one.\n\nwe define `node a` are directly infected by `node b` if `node a` will be infected by `node b` `without through any other infected node`.\n\nFor `node 1`, it will be directly infected by `node 0` and `node 4`,(0->1, 4->3->2->1)\nFor `node 2`, it is same as `node 1`(0->1->2, 4->3->2)\nFor `node 3`, it is same as `node 1`\nFor `node 5`, it is same as `node 1`\nFor `node 6`, it will be directly infected by `node 4`. (4 - > 6)\n\nfor node [1,2,3,5], even if we delete one node from the initial infected node, it will be infected by another node in the end.\n\nSo, a node may be safe if and only if it\'s just `directly` infected by one node. I called it `s_node`\nwe can use bfs solution to find the all `s_node`, and store the only node that can infect it.\nFinally, we count which node in the stored node appears the most.\n\n```python\n# 492ms\nimport collections\nclass Solution(object):\n    def minMalwareSpread(self, graph, initial):\n        n = len(graph)\n        d = collections.defaultdict(list)\n        for init in initial:\n            vis = set(initial)\n            Q = collections.deque([init])\n            while Q:\n                infect = Q.popleft()\n                for node in range(len(graph[infect])):\n                    if graph[infect][node] == 0: continue\n                    if node in vis: continue\n                    vis.add(node)\n                    d[node].append(init)\n                    Q.append(node)\n        # count the most frequent node\n        res = [0] * n\n        for key in d:\n            if len(d[key]) == 1:\n                res[d[key][0]] += 1\n        if max(res) == 0: return min(initial)\n        return res.index(max(res))\n```
[]