返回介绍

solution / 0900-0999 / 0924.Minimize Malware Spread / README_EN

发布于 2024-06-17 01:03:33 字数 7047 浏览 0 评论 0 收藏 0

924. Minimize Malware Spread

中文文档

Description

You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.

Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.

Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove exactly one node from initial.

Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it might still be infected later due to the malware spread.

 

Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
Output: 1

 

Constraints:

  • n == graph.length
  • n == graph[i].length
  • 2 <= n <= 300
  • graph[i][j] is 0 or 1.
  • graph[i][j] == graph[j][i]
  • graph[i][i] == 1
  • 1 <= initial.length <= n
  • 0 <= initial[i] <= n - 1
  • All the integers in initial are unique.

Solutions

Solution 1

class Solution:
  def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
    n = len(graph)
    p = list(range(n))
    size = [1] * n

    def find(x):
      if p[x] != x:
        p[x] = find(p[x])
      return p[x]

    for i in range(n):
      for j in range(i + 1, n):
        if graph[i][j] == 1:
          pa, pb = find(i), find(j)
          if pa == pb:
            continue
          p[pa] = pb
          size[pb] += size[pa]

    mi = inf
    res = initial[0]
    initial.sort()
    for i in range(len(initial)):
      t = 0
      s = set()
      for j in range(len(initial)):
        if i == j:
          continue
        if find(initial[j]) in s:
          continue
        s.add(find(initial[j]))
        t += size[find(initial[j])]
      if mi > t:
        mi = t
        res = initial[i]
    return res
class Solution {
  private int[] p;

  public int minMalwareSpread(int[][] graph, int[] initial) {
    int n = graph.length;
    p = new int[n];
    for (int i = 0; i < n; ++i) {
      p[i] = i;
    }
    int[] size = new int[n];
    Arrays.fill(size, 1);
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (graph[i][j] == 1) {
          int pa = find(i), pb = find(j);
          if (pa == pb) {
            continue;
          }
          p[pa] = pb;
          size[pb] += size[pa];
        }
      }
    }
    int mi = Integer.MAX_VALUE;
    int res = initial[0];
    Arrays.sort(initial);
    for (int i = 0; i < initial.length; ++i) {
      int t = 0;
      Set<Integer> s = new HashSet<>();
      for (int j = 0; j < initial.length; ++j) {
        if (i == j) {
          continue;
        }
        if (s.contains(find(initial[j]))) {
          continue;
        }
        s.add(find(initial[j]));
        t += size[find(initial[j])];
      }
      if (mi > t) {
        mi = t;
        res = initial[i];
      }
    }
    return res;
  }

  private int find(int x) {
    if (p[x] != x) {
      p[x] = find(p[x]);
    }
    return p[x];
  }
}
class Solution {
public:
  vector<int> p;

  int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
    int n = graph.size();
    p.resize(n);
    for (int i = 0; i < n; ++i) p[i] = i;
    vector<int> size(n, 1);
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        if (graph[i][j]) {
          int pa = find(i), pb = find(j);
          if (pa == pb) continue;
          p[pa] = pb;
          size[pb] += size[pa];
        }
      }
    }
    int mi = 400;
    int res = initial[0];
    sort(initial.begin(), initial.end());
    for (int i = 0; i < initial.size(); ++i) {
      int t = 0;
      unordered_set<int> s;
      for (int j = 0; j < initial.size(); ++j) {
        if (i == j) continue;
        if (s.count(find(initial[j]))) continue;
        s.insert(find(initial[j]));
        t += size[find(initial[j])];
      }
      if (mi > t) {
        mi = t;
        res = initial[i];
      }
    }
    return res;
  }

  int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
  }
};
var p []int

func minMalwareSpread(graph [][]int, initial []int) int {
  n := len(graph)
  p = make([]int, n)
  size := make([]int, n)
  for i := 0; i < n; i++ {
    p[i] = i
    size[i] = 1
  }
  for i := 0; i < n; i++ {
    for j := i + 1; j < n; j++ {
      if graph[i][j] == 1 {
        pa, pb := find(i), find(j)
        if pa == pb {
          continue
        }
        p[pa] = pb
        size[pb] += size[pa]
      }
    }
  }
  mi := 400
  res := initial[0]
  sort.Ints(initial)
  for i := 0; i < len(initial); i++ {
    t := 0
    s := make(map[int]bool)
    for j := 0; j < len(initial); j++ {
      if i == j {
        continue
      }
      if s[find(initial[j])] {
        continue
      }
      s[find(initial[j])] = true
      t += size[find(initial[j])]
    }
    if mi > t {
      mi = t
      res = initial[i]
    }
  }
  return res
}

func find(x int) int {
  if p[x] != x {
    p[x] = find(p[x])
  }
  return p[x]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文