924. Minimize Malware Spread

In a network of nodes, each node i is directly connected to another node j if and only 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 one node from the initial list.  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 may still be infected later as a result of 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

 

Note:

  • 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

Rust Solution

struct Solution;

struct UnionFind {
    parent: Vec<usize>,
    n: usize,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        let parent = (0..n).collect();
        UnionFind { parent, n }
    }

    fn find(&mut self, i: usize) -> usize {
        let j = self.parent[i];
        if i == j {
            i
        } else {
            let k = self.find(j);
            self.parent[i] = k;
            k
        }
    }

    fn union(&mut self, mut i: usize, mut j: usize) {
        i = self.find(i);
        j = self.find(j);
        if i != j {
            self.parent[i] = j;
            self.n -= 1;
        }
    }
}

impl Solution {
    fn min_malware_spread(graph: Vec<Vec<i32>>, initial: Vec<i32>) -> i32 {
        let n = graph.len();
        let mut uf = UnionFind::new(n);
        let mut initial: Vec<usize> = initial.into_iter().map(|i| i as usize).collect();
        for i in 0..n {
            for j in i + 1..n {
                if graph[i][j] == 1 {
                    uf.union(i, j);
                }
            }
        }
        let mut group_size: Vec<usize> = vec![0; n];
        for i in 0..n {
            group_size[uf.find(i)] += 1;
        }
        let mut initial_groups: Vec<usize> = vec![0; n];
        for &i in initial.iter() {
            let gid = uf.find(i as usize);
            initial_groups[gid] += 1;
        }
        initial.sort_unstable();
        let mut res = (0, initial[0]);
        for i in initial {
            let gid = uf.find(i as usize);
            if group_size[gid] > res.0 && initial_groups[gid] == 1 {
                res = (group_size[gid], i);
            }
        }
        res.1 as i32
    }
}

#[test]
fn test() {
    let graph = vec_vec_i32![[1, 1, 0], [1, 1, 0], [0, 0, 1]];
    let initial = vec![0, 1];
    let res = 0;
    assert_eq!(Solution::min_malware_spread(graph, initial), res);
    let graph = vec_vec_i32![[1, 0, 0], [0, 1, 0], [0, 0, 1]];
    let initial = vec![0, 2];
    let res = 0;
    assert_eq!(Solution::min_malware_spread(graph, initial), res);
    let graph = vec_vec_i32![[1, 1, 1], [1, 1, 1], [1, 1, 1]];
    let initial = vec![1, 2];
    let res = 1;
    assert_eq!(Solution::min_malware_spread(graph, initial), res);
}

Having problems with this solution? Click here to submit an issue on github.