5
public int find(int x) {6
if (parent[x] == x) return x;7
int f = find(parent[x]);12
void merge(int x, int y) {13
if (size[x] > size[y]) {22
public int minMalwareSpread(int[][] graph, int[] initial) {27
// putting initially infected nodes in hashset to ignore them while making graph28
HashSet<Integer> hs = new HashSet<>();29
for (int a : initial) {32
// initializing Parent for DSU33
for (int i = 0; i < parent.length; i++) {37
// constructing groups DSU38
for (int i = 0; i < graph.length; i++) {39
for (int j = 0; j < graph[0].length; j++) {40
if (graph[i][j] == 1 && !hs.contains(i) && !hs.contains(j)) {44
merge(p1, p2); // merging if they are not already49
// Storing initial Nodes vs parents of connected node...if there are multiple edge to same50
// component then we will get the same parent twice ...so we will take hashset for that purpose.51
HashMap<Integer, HashSet<Integer>> map = new HashMap<>();52
// We need to ensure increase the count whenever a parent is influenced by initial53
// Nodes...because if it influenced by more than one infectious node then it would still remain55
int[] infected = new int[n];56
for (int e : initial) {57
map.put(e, new HashSet<>());58
for (int j = 0; j < n; j++) {59
// in adjaceny graph[i][i] is always one so we will ignore that60
if (!hs.contains(j) && e != j && graph[e][j] == 1) {62
if (!map.get(e).contains(p)) {72
for (int e : initial) {73
HashSet<Integer> par = map.get(e);76
if (infected[p] == 1) { // add to total only if influenced by only one infectious node.82
ans = Math.min(ans, e);90
if (ans != -1) return ans;91
// for returining smallest element.