在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ (This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of nodes, each node Some nodes Suppose 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 Example 1: Input: graph = [0,1]
Output: 0
Example 2: Input: graph = [0,1]
Output: 1
Example 3: Input: graph = [0,1]
Output: 1
Note:
(这个问题与 尽量减少恶意软件的传播 是一样的,不同之处用粗体表示。) 在节点网络中,只有当 一些节点 假设 我们可以从初始列表中删除一个节点,并完全移除该节点以及从该节点到任何其他节点的任何连接。如果移除这一节点将最小化 示例 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 提示:
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 }
|
请发表评论