928. Minimize Malware Spread II

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjucor/article/details/83240042

(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如此,二分如此

猜你喜欢

转载自blog.csdn.net/zjucor/article/details/83240042