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

Idea: seeking the minimum, actually seeking the maximum, because malware may be removed after repeated infections, then after connecting the graph, if one of the two linked nodes is removed, it actually has no effect, because it will still be infected, then only root It is malware with only one value, and it will have an impact after that thing is removed. Then the smallest M is the problem, that is, the largest malware node is the most infected, and this root is only infected because he is alone. Use the count function to count the number of root infections after entering init to find out the connect component that was infected by him alone, and remove it. Of course, the node with the largest count, from small to large, just sorts the initial array.

class Solution {
    private class UnionFind {
        private int[] father;
        private int[] size;
        private int count;
        
        public UnionFind(int n) {
            this.father = new int[n+1];
            this.size = new int[n+1];
            for(int i = 0; i <= n; i++) {
                father[i] = i;
                size[i] = 1;
            }
            this.count = n;
        }
        
        public int find(int x) {
            int j = x;
            while(father[j] != j) {
                j = father[j];
            }
            //path compression;
            while(j != x) {
                int fx = father[x];
                father[x] = j;
                x = fx;
            }
            return j;
        }
        
        public void union(int a, int b) {
            int root_a = find(a);
            int root_b = find(b);
            if(root_a != root_b) {
                father[root_a] = root_b;
                size[root_b] += size[root_a];
                count--;
            }
        }
        
        public int[] getSizeArray() {
            return this.size;
        }
    }
    
    public int minMalwareSpread(int[][] graph, int[] initial) {
        int n = graph.length;
        UnionFind uf = new UnionFind(n);
        for(int i = 0; i < n; i++) {
            for(int j = i+1; j < n; j++) {
                if(graph[i][j] == 1) {
                    uf.union(i, j);
                }
            }
        }
        
        int[] size = uf.getSizeArray();
        int[] malware = new int[n];
        for(Integer i: initial) {
            malware[uf.find(i)]++;
        }
        Arrays.sort(initial);
        
        int maxweight = 0;
        int index = initial[0];
       
        for(Integer i: initial) {
            if(malware[uf.find(i)] == 1) {
                int weight = size[uf.find(i)];
                if(weight > maxweight) {
                    maxweight = weight;
                    index = i;
                }
            }
        }
        return index;
    }
}

 

Published 710 original articles · Like 13 · Visits 190,000+

Guess you like

Origin blog.csdn.net/u013325815/article/details/105342512