跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • [leetcode/lintcode 题解] 尽量减少恶意软件的传播 II
未分類
24 6 月 2020

[leetcode/lintcode 题解] 尽量减少恶意软件的传播 II

[leetcode/lintcode 题解] 尽量减少恶意软件的传播 II

資深大佬 : hakunamatata11 4

(这个问题与 尽量减少恶意软件的传播 是一样的,不同之处用粗体表示。) 在节点网络中,只有当 graph[i][j] = 1 时,每个节点 i 能够直接连接到另一个节点 j 。

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

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

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

  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

在线评测地址: https://www.lintcode.com/problem/minimize-malware-spread-ii/?utm_source=sc-csdn-fks

样例 1:

输入:graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] 输出:0 解释:移除 0 然后只有 1 被感染。 

样例 2:

输入:graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1] 输出:1 解释:移除 1 然后只有 0 被感染。 

考点:

  • 图论
  • 搜索

题解:要想把一个点从本来要被感染的状态变成不被感染,那么需要把能把它感染的结点全部删除!!!

如果一个结点能够被两个或更多感染结点从不同的路径感染,那么这个结点是不可能安全的。如下图所示,其中黄色结点是初始被感染结点: [leetcode/lintcode 题解] 尽量减少恶意软件的传播 II

class Solution {     public int minMalwareSpread(int[][] graph, int[] initial) {         Arrays.sort(initial);                  Set<Integer> mal = new HashSet<>();         for(int n : initial)             mal.add(n);                  int max = -1, ret = -1;         for(int n : initial){             int save = 0;             Set<Integer> visited = new HashSet<>();             visited.add(n);             for(int i = 0; i<graph.length; i++){                 if(i != n && graph[n][i] == 1){                     int temp = dfs(i, visited, mal, graph);                     if(temp < 0) continue; // encountered malware during exploration, meaning this whole branch doesn't count/contribute                     save += temp;                 }             }             if(save > max){                  ret = n;                 max = save;             }         }         return ret;     }          private int dfs(int n, Set<Integer> visited, Set<Integer> mal, int[][] graph){         if(visited.contains(n)) return 0;         if(mal.contains(n)) return -1;         visited.add(n);                  int ret = 1; // current node saved (at least for now)         for(int i = 0; i<graph.length; i++){             if(i != n && graph[n][i] == 1){                 int temp = dfs(i, visited, mal, graph);                 if(temp == -1) {                     mal.add(n); // has neighbor malware, marked as malware as well                     return -1; // return -1, indicating there's malware downstream in this branch, whole branch unqualified!                 }                 ret += temp;             }         }         return ret;     } } 

更多语言代码参见:https://www.jiuzhang.com/solution/minimize-malware-spread-ii/?utm_source=sc-csdn-fks

大佬有話說 (0)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具