Minimize Malware Spread

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:


Solution:

class Solution {
    class UF {
        int[] parent;
        int[] size;
        
        public UF(int n) {
            parent = new int[n];
            size = new int[n];
            
            for (int i = 0; i < n; i ++) {
                parent[i] = i;
                size[i] = 1;
            }
        }
        
        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) return;
            if (size[rootX] >= size[rootY]) {
                parent[rootY] = rootX;
                size[rootX] += size[rootY];
            } else {
                parent[rootX] = rootY;
                size[rootY] += size[rootX];;
            }
        }
        
        public int find(int x) {
            while (x != parent[x]) {
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }
    }
    
    public int minMalwareSpread(int[][] graph, int[] initial) {
        int n = graph.length;
        UF uf = new UF(n);
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                if (i != j && graph[i][j] == 1) {
                    uf.union(i, j);
                }
            }
        }
        int maxSize = 0;
        int r = -1;
        // System.out.println(Arrays.toString(uf.size));
        Map<Integer, Integer> count = new HashMap();
        Set<Integer> meaningless = new HashSet();
        
        for (int i : initial) {
            int rootI = uf.find(i);
            count.put(rootI, count.getOrDefault(rootI, 0) + 1);
        }        
        
        for (int i : initial) {
            int rootI = uf.find(i);
            if (count.get(rootI) > 1) {
                meaningless.add(i);
            }
        }

        for (int i : initial) {
            if (meaningless.contains(i)) continue;
            int rootI = uf.find(i);
            if (uf.size[rootI] > maxSize) {
                maxSize = uf.size[rootI];
                r = i;
            } else if (uf.size[rootI] == maxSize && r > i) {
                r = i;
            } 
        }
        if (r != -1) return r;
        Arrays.sort(initial);
        return initial[0];
    }
}