题目描述:
在节点网络中,只有当 graph[i][j] = 1 时,每个节点 i 能够直接连接到另一个节点 j。
一些节点 initial 最初被恶意软件感染。只要两个节点直接连接,且其中至少一个节点受到恶意软件的感染,那么两个节点都将被恶意软件感染。这种恶意软件的传播将继续,直到没有更多的节点可以被这种方式感染。
假设 M(initial) 是在恶意软件停止传播之后,整个网络中感染恶意软件的最终节点数。
我们可以从初始列表中删除一个节点。如果移除这一节点将最小化 M(initial), 则返回该节点。如果有多个节点满足条件,就返回索引最小的节点。
请注意,如果某个节点已从受感染节点的列表 initial 中删除,它以后可能仍然因恶意软件传播而受到感染。
示例 1:
输入:graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
输出:0
示例 2:
输入:graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
输出:0
示例 3:
输入:graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
输出:1
提示:
1 < graph.length = graph[0].length <= 300
0 <= graph[i][j] == graph[j][i] <= 1
graph[i][i] = 1
1 <= initial.length < graph.length
0 <= initial[i] < graph.length
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimize-malware-spread
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题有点懵,其实比较简单
先使用dfs
先将每个节点用一个颜色表示出来,然后计数,次数最多的就是影响最多的,那么我们将其选择出来返回即可
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
// 1. Color each component.
// colors[node] = the color of this node.
int N = graph.length;
int[] colors = new int[N];
Arrays.fill(colors, -1);
int C = 0;
// 初始化每一个节点,将每个节点赋予颜色,放在color数组中
for (int node = 0; node < N; ++node)
if (colors[node] == -1)
dfs(graph, colors, node, C++);
// 2. Size of each color.
// 统计每一个颜色出现的次数,放在size数组中
int[] size = new int[C];
for (int color: colors)
size[color]++;
// 3. Find unique colors
// 统计initial中出现节点的数量,colorCount表示的是出现在initial中的节点的颜色++.
// 4. Answer
// 计算每一个出现node的个数
int ans = Integer.MAX_VALUE;
for (int node: initial) {
int c = colors[node];
if (ans == Integer.MAX_VALUE)
ans = node;
else if (size[c] > size[colors[ans]])
ans = node;
else if (size[c] == size[colors[ans]] && node < ans)
ans = node;
}
if (ans == Integer.MAX_VALUE)
for (int node: initial)
ans = Math.min(ans, node);
return ans;
}
public void dfs(int[][] graph, int[] colors, int node, int color) {
colors[node] = color;
for (int nei = 0; nei < graph.length; ++nei)
if (graph[node][nei] == 1 && colors[nei] == -1)
dfs(graph, colors, nei, color);
}
}
一天过后再看上面的这个代码其实是有问题的,因为对于[[1,0,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0,0],[0,0,1,0,1,0,1,0,0],[0,0,0,1,0,0,0,0,0],[0,0,1,0,1,0,0,0,0],[0,0,0,0,0,1,0,0,0],[0,0,1,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,0],[0,0,0,0,0,0,0,0,1]] [6,0,4] 对于该测试用例,使用dfs的结果应该是0,而上面的dfs方法是4,dfs有问题的,因为2连接的是4和6,如果删除4指挥使得结果减一和6、0一样因此结果是0
因此测试用例的强度不够
考虑改变一下思路,每次都遍历:
class Solution {
int gets = 0;
public int minMalwareSpread(int[][] graph, int[] initial) {
// 1. Color each component.
// colors[node] = the color of this node.
int N = graph.length;
int tem = N;
Arrays.sort(initial);
// 尝试每次移除其中一个病毒,然后计算被感染的
int ans = initial[0];
for (int i = 0; i < initial.length; i++) {
int i1 = initial[i];
// 开始侵染
int tem1 = 0;
int colors[] = new int[N];
Arrays.fill(colors, -1);
gets = 0;
for (int j = 0; j < initial.length; j++) {
if(i == j){
continue;
}
for (int k = 0; k < N; k++) {
if (graph[initial[j]][k] == 1 && colors[k] == -1) {
tem1 += dfs(graph, colors, k, 0);
}
}
}
if (gets < tem) {
tem = gets;
ans = initial[i];
}
}
return ans;
}
public int dfs(int[][] graph, int[] colors, int node, int color) {
gets++;
colors[node] = 0;
int t = 0;
for (int nei = 0; nei < graph.length; ++nei) {
if (graph[node][nei] == 1 && colors[nei] == -1) {
colors[nei] = 0;
t += dfs(graph, colors, nei, color);
}
}
return t + 1;
}
}
或者使用并查集,待理解:
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
int N = graph.length;
DSU dsu = new DSU(N);
for (int i = 0; i < N; ++i)
for (int j = i+1; j < N; ++j)
if (graph[i][j] == 1)
dsu.union(i, j);
int[] count = new int[N];
for (int node: initial)
count[dsu.find(node)]++;
int ans = -1, ansSize = -1;
for (int node: initial) {
int root = dsu.find(node);
if (count[root] == 1) { // unique color
int rootSize = dsu.size(root);
if (rootSize > ansSize) {
ansSize = rootSize;
ans = node;
} else if (rootSize == ansSize && node < ans) {
ansSize = rootSize;
ans = node;
}
}
}
if (ans == -1) {
ans = Integer.MAX_VALUE;
for (int node: initial)
ans = Math.min(ans, node);
}
return ans;
}
}
class DSU {
int[] p, sz;
DSU(int N) {
p = new int[N];
for (int x = 0; x < N; ++x)
p[x] = x;
sz = new int[N];
Arrays.fill(sz, 1);
}
public int find(int x) {
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
public void union(int x, int y) {
int xr = find(x);
int yr = find(y);
p[xr] = yr;
sz[yr] += sz[xr];
}
public int size(int x) {
return sz[find(x)];
}
}
作者:LeetCode
链接:https://leetcode-cn.com/problems/minimize-malware-spread/solution/jin-liang-jian-shao-e-yi-ruan-jian-de-chuan-bo-b-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。