{"id":124345,"date":"2020-06-24T01:25:18","date_gmt":"2020-06-23T17:25:18","guid":{"rendered":"http:\/\/4563.org\/?p=124345"},"modified":"2020-06-24T01:25:18","modified_gmt":"2020-06-23T17:25:18","slug":"leetcode-lintcode-%e9%a2%98%e8%a7%a3-%e5%b0%bd%e9%87%8f%e5%87%8f%e5%b0%91%e6%81%b6%e6%84%8f%e8%bd%af%e4%bb%b6%e7%9a%84%e4%bc%a0%e6%92%ad-ii","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=124345","title":{"rendered":"[leetcode\/lintcode \u9898\u89e3] \u5c3d\u91cf\u51cf\u5c11\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad II"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [leetcode\/lintcode \u9898\u89e3] \u5c3d\u91cf\u51cf\u5c11\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad II               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>(\u8fd9\u4e2a\u95ee\u9898\u4e0e \u5c3d\u91cf\u51cf\u5c11\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad \u662f\u4e00\u6837\u7684\uff0c\u4e0d\u540c\u4e4b\u5904\u7528\u7c97\u4f53\u8868\u793a\u3002) \u5728\u8282\u70b9\u7f51\u7edc\u4e2d\uff0c\u53ea\u6709\u5f53 graph[i][j] = 1 \u65f6\uff0c\u6bcf\u4e2a\u8282\u70b9 i \u80fd\u591f\u76f4\u63a5\u8fde\u63a5\u5230\u53e6\u4e00\u4e2a\u8282\u70b9 j \u3002<\/p>\n<p>\u4e00\u4e9b\u8282\u70b9 initial \u6700\u521d\u88ab\u6076\u610f\u8f6f\u4ef6\u611f\u67d3\u3002\u53ea\u8981\u4e24\u4e2a\u8282\u70b9\u76f4\u63a5\u8fde\u63a5\uff0c\u4e14\u5176\u4e2d\u81f3\u5c11\u4e00\u4e2a\u8282\u70b9\u53d7\u5230\u6076\u610f\u8f6f\u4ef6\u7684\u611f\u67d3\uff0c\u90a3\u4e48\u4e24\u4e2a\u8282\u70b9\u90fd\u5c06\u88ab\u6076\u610f\u8f6f\u4ef6\u611f\u67d3\u3002\u8fd9\u79cd\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad\u5c06\u7ee7\u7eed\uff0c\u76f4\u5230\u6ca1\u6709\u66f4\u591a\u7684\u8282\u70b9\u53ef\u4ee5\u88ab\u8fd9\u79cd\u65b9\u5f0f\u611f\u67d3\u3002<\/p>\n<p>\u5047\u8bbe M(initial) \u662f\u5728\u6076\u610f\u8f6f\u4ef6\u505c\u6b62\u4f20\u64ad\u4e4b\u540e\uff0c\u6574\u4e2a\u7f51\u7edc\u4e2d\u611f\u67d3\u6076\u610f\u8f6f\u4ef6\u7684\u6700\u7ec8\u8282\u70b9\u6570\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4ece\u521d\u59cb\u5217\u8868\u4e2d\u5220\u9664\u4e00\u4e2a\u8282\u70b9\uff0c\u5e76\u5b8c\u5168\u79fb\u9664\u8be5\u8282\u70b9\u4ee5\u53ca\u4ece\u8be5\u8282\u70b9\u5230\u4efb\u4f55\u5176\u4ed6\u8282\u70b9\u7684\u4efb\u4f55\u8fde\u63a5\u3002\u5982\u679c\u79fb\u9664\u8fd9\u4e00\u8282\u70b9\u5c06\u6700\u5c0f\u5316 M(initial)\uff0c \u5219\u8fd4\u56de\u8be5\u8282\u70b9\u3002\u5982\u679c\u6709\u591a\u4e2a\u8282\u70b9\u6ee1\u8db3\u6761\u4ef6\uff0c\u5c31\u8fd4\u56de\u7d22\u5f15\u6700\u5c0f\u7684\u8282\u70b9\u3002<\/p>\n<ol>\n<li>&lt; graph.length = graph[0].length &lt;= 300<\/li>\n<li>0&lt;= graph[i][j] == graph[j][i] &lt;= 1<\/li>\n<li>graph[i][i] = 1<\/li>\n<li>1 &lt;= initial.length &lt; graph.length<\/li>\n<li>0 &lt;= initial[i] &lt; graph.length<\/li>\n<\/ol>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740: https:\/\/www.lintcode.com\/problem\/minimize-malware-spread-ii\/?utm_source=sc-csdn-fks<\/p>\n<p>\u6837\u4f8b 1:<\/p>\n<pre><code>\u8f93\u5165\uff1agraph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] \u8f93\u51fa\uff1a0 \u89e3\u91ca\uff1a\u79fb\u9664 0 \u7136\u540e\u53ea\u6709 1 \u88ab\u611f\u67d3\u3002 <\/code><\/pre>\n<p>\u6837\u4f8b 2:<\/p>\n<pre><code>\u8f93\u5165\uff1agraph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1] \u8f93\u51fa\uff1a1 \u89e3\u91ca\uff1a\u79fb\u9664 1 \u7136\u540e\u53ea\u6709 0 \u88ab\u611f\u67d3\u3002 <\/code><\/pre>\n<p>\u8003\u70b9\uff1a<\/p>\n<ul>\n<li>\u56fe\u8bba<\/li>\n<li>\u641c\u7d22<\/li>\n<\/ul>\n<p>\u9898\u89e3\uff1a\u8981\u60f3\u628a\u4e00\u4e2a\u70b9\u4ece\u672c\u6765\u8981\u88ab\u611f\u67d3\u7684\u72b6\u6001\u53d8\u6210\u4e0d\u88ab\u611f\u67d3\uff0c\u90a3\u4e48\u9700\u8981\u628a\u80fd\u628a\u5b83\u611f\u67d3\u7684\u7ed3\u70b9\u5168\u90e8\u5220\u9664\uff01\uff01\uff01<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u7ed3\u70b9\u80fd\u591f\u88ab\u4e24\u4e2a\u6216\u66f4\u591a\u611f\u67d3\u7ed3\u70b9\u4ece\u4e0d\u540c\u7684\u8def\u5f84\u611f\u67d3\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7ed3\u70b9\u662f\u4e0d\u53ef\u80fd\u5b89\u5168\u7684\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5176\u4e2d\u9ec4\u8272\u7ed3\u70b9\u662f\u521d\u59cb\u88ab\u611f\u67d3\u7ed3\u70b9\uff1a <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/06\/20200627_5ef7a9a15fe34.png\" alt=\"[leetcode\/lintcode \u9898\u89e3] \u5c3d\u91cf\u51cf\u5c11\u6076\u610f\u8f6f\u4ef6\u7684\u4f20\u64ad II\" \/><\/p>\n<pre><code>class Solution {     public int minMalwareSpread(int[][] graph, int[] initial) {         Arrays.sort(initial);                  Set&lt;Integer&gt; mal = new HashSet&lt;&gt;();         for(int n : initial)             mal.add(n);                  int max = -1, ret = -1;         for(int n : initial){             int save = 0;             Set&lt;Integer&gt; visited = new HashSet&lt;&gt;();             visited.add(n);             for(int i = 0; i&lt;graph.length; i++){                 if(i != n &amp;&amp; graph[n][i] == 1){                     int temp = dfs(i, visited, mal, graph);                     if(temp &lt; 0) continue; \/\/ encountered malware during exploration, meaning this whole branch doesn't count\/contribute                     save += temp;                 }             }             if(save &gt; max){                  ret = n;                 max = save;             }         }         return ret;     }          private int dfs(int n, Set&lt;Integer&gt; visited, Set&lt;Integer&gt; 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&lt;graph.length; i++){             if(i != n &amp;&amp; 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;     } } <\/code><\/pre>\n<p>\u66f4\u591a\u8bed\u8a00\u4ee3\u7801\u53c2\u89c1\uff1ahttps:\/\/www.jiuzhang.com\/solution\/minimize-malware-spread-ii\/?utm_source=sc-csdn-fks<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[leetcode\/lintcod&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124345"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=124345"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124345\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124345"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}