题目地址:

https://www.lintcode.com/problem/minimize-malware-spread/description

给定一个nnn个节点的无向图,以邻接矩阵给出。每个节点代表一个网络中的节点。再给定一个数组AAA,A[i]A[i]A[i]表示节点iii被感染了(下文称AAA里的点是”感染源“)。当一个节点被感染了,它所在的连通块的所有节点都会被感染。现在允许选取一个“感染源”,将其变为”未感染“。问将哪个节点变为”未感染“可以使得剩余节点的总的被感染节点数量最小。如果有多个答案,则返回编号最小的节点。

思路可以参考https://blog.csdn.net/qq_46105170/article/details/113488695,那里是用并查集做的。这里采用DFS来做。

依然是要找到感染源只有一个的连通块,那么这样的连通块的最大size对应的感染源就是答案(多个解则取编号最小者)。如果不存在这样的连通块,则直接取感染源的编号最小者。这里可以用DFS将每个连通块染色,然后统计每个颜色的节点数,和每个颜色的污染源数。相同颜色的点显然就是个连通块。然后按上面逻辑求解即可。代码如下:

import java.util.Arrays;public class Solution {/*** @param graph:   the node graph* @param initial: the infected node* @return: the node index*/public int minMalwareSpread(int[][] graph, int[] initial) {// write your code here.int n = graph.length;// colors[i]存编号为i的节点被染上的颜色int[] colors = new int[n];Arrays.fill(colors, -1);int color = 0;for (int i = 0; i < n; i++) {if (colors[i] == -1) {dfs(i, color++, colors, graph);}}// size[i]存被染了颜色i的节点有多少个int[] size = new int[color];for (int i : colors) {size[i]++;}// malCount[i]存被染了颜色i的连通块里的感染源有多少个int[] malCount = new int[color];for (int i : initial) {malCount[colors[i]]++;}int res = -1, maxSaved = 0;for (int v : initial) {// 略过含多个污染源的连通块if (malCount[colors[v]] != 1) {continue;}if (size[colors[v]] > maxSaved) {maxSaved =size[colors[v]];res = v;} else if (size[colors[v]] == maxSaved) {res = Math.min(res, v);}}if (res == -1) {res = n;for (int i : initial) {res = Math.min(res, i);}}return res;}private void dfs(int v, int color, int[] colors, int[][] graph) {colors[v] = color;for (int next = 0; next < graph[v].length; next++) {if (graph[v][next] == 1 && colors[next] == -1) {dfs(next, color, colors, graph);}}}
}

时间复杂度O(n2)O(n^2)O(n2),空间O(n)O(n)O(n)。

【Lintcode】1718. Minimize Malware Spread相关推荐

  1. 【LintCode】算法题 1443. 最长AB子串

    描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...

  2. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  3. 【Lintcode】1413. Tree

    题目地址: https://www.lintcode.com/problem/tree/description 给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树).要求 ...

  4. 【Lintcode】1645. Least Subsequences

    题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...

  5. 【Lintcode】1375. Substring With At Least K Distinct Characters

    题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...

  6. 【Lintcode】1647. Path Search

    题目地址: https://www.lintcode.com/problem/path-search/description 给定一个无向图,再给定两个点SSS和TTT,求所有SSS到TTT的简单路径 ...

  7. 【Lintcode】1799. Sequence Summation

    题目地址: https://www.lintcode.com/problem/sequence-summation/description 给定三个数i,j,ki,j,ki,j,k,计算i+(i+1) ...

  8. 【Lintcode】1367. Police Distance

    题目地址: https://www.lintcode.com/problem/1367/ 给定一个m×nm\times nm×n的矩阵,只含1,0,−11,0,-11,0,−1,111表示警察,000 ...

  9. 【Lintcode】444. Graph Valid Tree II

    题目地址: https://www.lintcode.com/problem/444/ 要求设计一个数据结构,可以做如下两个操作: 1.void addEdge(int a, int b)在aaa与b ...

最新文章

  1. iOS系统 应用闪退或图标消失的解决办法
  2. Php的if自动转换类型,php输出数据字段类型自动转换函数
  3. 数据结构——图的深度遍历
  4. maven的package与install命令区别
  5. 强化学习之Q-learning简介
  6. 微信云开发实现微信支付与退款的两种思路
  7. 欢迎使用云-“您的Apple ID已被禁用。”
  8. 一种低成本的机械动平衡方法 郑氏橡皮泥动平衡法
  9. 电脑本地进行演讲者模式预览快捷键
  10. 如何给Word中的图片添加题注
  11. 教师计算机西沃培训心得,希沃电子白板学习心得体会
  12. Android 强制关闭软键盘/修改软键盘状态——弹出或关闭
  13. xgboost 中的二阶导数为什么收敛更快?
  14. Server Tomcat v7.0 Server at localhost failed to start.
  15. Mac安装gensim报错:No extention 巴拉巴拉
  16. Android——横幅通知
  17. [附源码]计算机毕业设计Python+uniapp汽车票管理系统APP65qfl(程序+lw+APP+远程部署)
  18. Kt学习笔记(九)数据类、封闭类
  19. 修改tomcat端口号并部署项目
  20. 春天猫rtsy_您最近见过春天吗?

热门文章

  1. Aura component cache clear set
  2. 冲刺!这篇 1658 页的《Java 面试突击核心讲》学明白保底年薪 30w
  3. 以太坊学习路线——(二、下)以太坊编程接口:web3.js
  4. IT十年人生过客1-开篇
  5. Typescript 笔记
  6. MongoDB权威指南读书笔记——CRUD
  7. 写程序关键是真正自己开始动手写,不要总是想着等全部弄懂了再写
  8. 超级计算机预测南方下雪,强寒潮南下在即,广东将要下雪?超级计算机:可能性不高...
  9. TCP time_wait 的存在意义
  10. MAC地址存在的意义