1. 程式人生 > >928. Minimize Malware Spread II

928. Minimize Malware Spread II

(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,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

 

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

思路:BFS沒錯,比賽一直WA,用的程式碼如下

class Solution:
    def minMalwareSpread(self, graph, initial):
        """
        :type graph: List[List[int]]
        :type initial: List[int]
        :rtype: int
        """
        initial.sort()
        mi,res=float('inf'),initial[0]
        
        def bfs(target):
            cnt=0
            vis=[False]*len(graph)
            for i in initial:
                if vis[i] or i==target: continue
                q=[i]
                while q:
                    s=q.pop()
                    vis[s]=True
                    cnt+=1
                    for t in range(len(graph)):
                        if graph[s][t] and not vis[t] and t!=target: q.append(t)
            return cnt
        
        for i in initial:
            t=bfs(i)
            print(i,t)
            if t<mi:
                mi=t
                res=i
        return res

因為寫的BFS,記錄vis的變數跟之前習慣的寫法不一樣,是直接放在pop自後(之前都是放到加入queue那部分)。

因為上次比賽類似的題目這樣寫可以AC,所以也沒太注意。但是對於這題來說,只是個致命的bug,可能出現:因為沒有及時的設定vis變數,導致在重複加入變數到queue中。

改回原來的方式寫BFS就OK了

class Solution:
    def minMalwareSpread(self, graph, initial):
        """
        :type graph: List[List[int]]
        :type initial: List[int]
        :rtype: int
        """
        initial.sort()
        mi,res=float('inf'),initial[0]
        
        def bfs(target):
            cnt=0
            vis=[False]*len(graph)
            for i in initial:
                if vis[i] or i==target: continue
                vis[i]=True
                cnt+=1
                q=[i]
                while q:
                    s=q.pop()
                    for t in range(len(graph)):
                        if graph[s][t] and not vis[t] and t!=target: 
                            vis[t]=True
                            cnt+=1
                            q.append(t)
            return cnt
        
        for i in initial:
            t=bfs(i)
#            print(i,t)
            if t<mi:
                mi=t
                res=i
        return res

s=Solution()
print(s.minMalwareSpread(graph = [[1,1,0,0],[1,1,0,0],[0,0,1,1],[0,0,1,1]], initial = [0,1,2,3]))
#print(s.minMalwareSpread(graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]))
#print(s.minMalwareSpread(graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]))
#print(s.minMalwareSpread(graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]))
#print(s.minMalwareSpread())
#print(s.minMalwareSpread())
#print(s.minMalwareSpread())
#            

之前那個題能過我也覺得是巧合,資料再強一點估計也得WA,所以最好還是按照標準的寫法來寫吧,BFS如此,二分如此