[Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9825788.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

(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

(这个问题与 尽量减少恶意软件的传播 是一样的,不同之处用粗体表示。)

在节点网络中,只有当 graph[i][j] = 1 时,每个节点 i 能够直接连接到另一个节点 j

一些节点 initial 最初被恶意软件感染。只要两个节点直接连接,且其中至少一个节点受到恶意软件的感染,那么两个节点都将被恶意软件感染。这种恶意软件的传播将继续,直到没有更多的节点可以被这种方式感染。

假设 M(initial) 是在恶意软件停止传播之后,整个网络中感染恶意软件的最终节点数。

我们可以从初始列表中删除一个节点,并完全移除该节点以及从该节点到任何其他节点的任何连接。如果移除这一节点将最小化 M(initial), 则返回该节点。如果有多个节点满足条件,就返回索引最小的节点。

 示例 1:

输出:graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
输入:0

示例 2:

输入:graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]
输出:1

示例 3:

输入:graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
输出:1

 提示:

  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

 1058ms

  1 class Solution {
  2     func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
  3         var n:Int = graph.count
  4         var arr = initial.sorted(by: <)
  5         
  6         var min:Int = 99999999
  7         var arg:Int = -1
  8         for i in arr
  9         {
 10             var linf:[Bool] = [Bool](repeating: false,count: n)
 11             var ds:DJSet =  DJSet(n)
 12             for j in 0..<n
 13             {
 14                 for k in (j + 1)..<n
 15                 {
 16                     if j == i || k == i {continue}
 17                     if graph[j][k] == 1 {ds.union(j,k)}
 18                     
 19                 }
 20             }
 21             for v in arr
 22             {
 23                 if v != i
 24                 {
 25                     linf[ds.root(v)] = true
 26                 }
 27             }
 28             var g:Int = 0
 29             for j in 0..<n
 30             {
 31                 if linf[ds.root(j)]
 32                 {
 33                     g += 1
 34                 }
 35             }
 36             if g < min
 37             {
 38                 min = g
 39                 arg = i
 40             }
 41             
 42         }
 43         return arg
 44     }
 45 }
 46 class DJSet
 47 {
 48     var upper:[Int] = [Int]()
 49     
 50     init(_ n:Int)
 51     {
 52         self.upper = [Int](repeating: -1,count: n)        
 53     }
 54     
 55     func root(_ x:Int) -> Int
 56     {
 57         if  upper[x] < 0
 58         {
 59             return x
 60         }
 61         else
 62         {
 63             upper[x] = root(upper[x])
 64         }
 65         return upper[x]
 66     }
 67     
 68     func equiv(_ x:Int,_ y:Int) -> Bool
 69     {
 70         return root(x) == root(y)
 71     }
 72     
 73     func union(_ x:Int,_ y:Int) -> Bool
 74     {
 75        var x = root(x)
 76        var y = root(y)
 77         if x != y
 78         {
 79             if upper[y] < upper[x]
 80             {
 81                 var d:Int = x
 82                 x = y
 83                 y = d
 84             }
 85             upper[x] += upper[y]
 86             upper[y] = x
 87         }
 88         return x == y
 89     }
 90     
 91     func count() -> Int
 92     {
 93         var ct:Int = 0
 94         for u in upper
 95         {
 96             if u < 0
 97             {
 98                 ct += 1
 99             }
100         }
101         return ct
102     }
103 }    

2020ms

 1 class Solution {
 2   var visited: Set<Int> = Set()
 3   var initial: Set<Int> = Set() 
 4   var graph : [[Int]] = []
 5   
 6   func traverse(_ pos: Int) -> Int {
 7     if visited.contains(pos) { 
 8       return 0 
 9     }
10     var result = 1
11     visited.insert(pos)
12     for i in 0..<graph.count {
13       if i != pos && graph[pos][i] == 1 {
14         if !visited.contains(i) {
15           result += traverse(i)
16         }
17       }
18     }
19     return result
20   }
21   
22   func countComponent(_ start: Int) -> Int {
23     return traverse(start)
24   }
25   
26   func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
27     self.graph = graph
28     self.initial = Set<Int>(initial)
29     
30     var mincnt = graph.count
31     var mini = -1
32     
33     for removed in initial.sorted() {
34       visited = Set()
35       visited.insert(removed)
36       var n = 0
37       for x in initial {
38         n += countComponent(x)
39       }
40       if n < mincnt {
41         mincnt = n
42         mini = removed
43       }
44     }
45 
46     return mini
47   }
48 }

2696ms

  1 class Solution {
  2     func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
  3         var n:Int = graph.count
  4         var arr = initial.sorted(by: <)
  5         
  6         var min:Int = 99999999
  7         var arg:Int = -1
  8         for i in arr
  9         {
 10             var linf:[Bool] = [Bool](repeating: false,count: n)
 11             var ds:DJSet =  DJSet(n)
 12             for j in 0..<n
 13             {
 14                 for k in (j + 1)..<n
 15                 {
 16                     if j == i || k == i {continue}
 17                     if graph[j][k] == 1 {ds.union(j,k)}
 18                     
 19                 }
 20             }
 21             for v in arr
 22             {
 23                 if v != i
 24                 {
 25                     linf[ds.root(v)] = true
 26                 }
 27             }
 28             var g:Int = 0
 29             for j in 0..<n
 30             {
 31                 if linf[ds.root(j)]
 32                 {
 33                     g += 1
 34                 }
 35             }
 36             if g < min
 37             {
 38                 min = g
 39                 arg = i
 40             }
 41             
 42         }
 43         return arg
 44     }
 45 }
 46 class DJSet
 47 {
 48     var upper:[Int] = [Int]()
 49     
 50     init(_ n:Int)
 51     {
 52         self.upper = [Int](repeating: -1,count: n)        
 53     }
 54     
 55     func root(_ x:Int) -> Int
 56     {
 57         if  upper[x] < 0
 58         {
 59             return x
 60         }
 61         else
 62         {
 63             upper[x] = root(upper[x])
 64         }
 65         return upper[x]
 66     }
 67     
 68     func equiv(_ x:Int,_ y:Int) -> Bool
 69     {
 70         return root(x) == root(y)
 71     }
 72     
 73     func union(_ x:Int,_ y:Int) -> Bool
 74     {
 75        var x = root(x)
 76        var y = root(y)
 77         if x != y
 78         {
 79             if upper[y] < upper[x]
 80             {
 81                 var d:Int = x
 82                 x = y
 83                 y = d
 84             }
 85             upper[x] += upper[y]
 86             upper[y] = x
 87         }
 88         return x == y
 89     }
 90     
 91     func count() -> Int
 92     {
 93         var ct:Int = 0
 94         for u in upper
 95         {
 96             if u < 0
 97             {
 98                 ct += 1
 99             }
100         }
101         return ct
102     }
103 }    
原文地址:https://www.cnblogs.com/strengthen/p/9825788.html