Easiest Solution
\n\n# Code\njava []\nclass Solution {\n    int[][] par;\n    \n    public int minMalwareSpread(int[][] graph, int[] initial) {\n    \tint n = graph.length;\n   
\n\n# Code\n```java []\nclass Solution {\n    int[][] par;\n    \n    public int minMalwareSpread(int[][] graph, int[] initial) {\n    \tint n = graph.length;\n        par = new int[n][2];\n        for(int i = 0; i < par.length; i++){\n            par[i][0] = i;\n            par[i][1] = 1;\n        }\n        \n        HashSet<Integer> set = new HashSet<>();\n        for(int p : initial)\n        \tset.add(p);\n        \n        for(int i = 0; i < n; i++) {\n        \tfor(int j = i + 1; j < n; j++) {\n        \t\tif(graph[i][j] == 1 && !set.contains(i) && !set.contains(j)) {\n        \t\t\tint li = find(i);\n        \t\t\tint lj = find(j);\n        \t\t\tif(li != lj){\n                        par[li][0] = lj;\n                        par[lj][1] += par[li][1];\n                    }\n        \t\t}\n        \t}\n        }\n        \n        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();\n        HashMap<Integer, HashSet<Integer>> conntoinf = new HashMap<>();\n        \n        for(int i : initial) {\n        \tmap.put(i, new HashSet<>());\n        \tfor(int j = 0; j < n; j++) {\n        \t\tif(graph[i][j] == 1 && i != j && !set.contains(j)) {\n        \t\t\tint lj = find(j);\n        \t\t\tmap.get(i).add(lj);\n                    if(conntoinf.containsKey(lj) == false)\n                        conntoinf.put(lj, new HashSet<>());\n        \t\t\tconntoinf.get(lj).add(i);\n        \t\t}\n        \t}\n        }\n        int ans = initial[0];\n        int max = -1;\n        \n        for(int p : initial) {\n        \tint val = 0;\n        \tfor(int l : map.get(p)) {\n        \t\tif(conntoinf.get(l).size() == 1)\n        \t\t\tval += par[l][1];\n        \t}\n            if(val >= max) {\n                if(val == max)\n                    ans = Math.min(p, ans);\n                else\n                    ans = p;\n                max = val;\n            }\n        }\n        \n        return ans;\n    }\n    \n    public int find(int x){\n        if(par[x][0] == x)\n            return x;\n        int temp = find(par[x][0]);\n        return par[x][0] = temp;\n    }\n}\n```\n```c++ []\nclass Solution {\npublic:     \n    void prepare(unordered_map<int, set<int>> &adj, vector<vector<int>>& graph)\n    {\n        int n = graph.size();\n        for(int i = 0 ; i < n ; i++)\n        {\n            for(int j = 0 ; j < n ; j++)\n            {\n                if(i != j && graph[i][j] == 1)\n                {\n                    adj[i].insert(j);\n                    adj[j].insert(i);\n                }\n            }\n        }\n    }\n    void dfs(int node, unordered_map<int, set<int>> &adj, vector<int> &vis, int *cnt)\n    {\n        vis[node] = 1;\n        *cnt = *cnt + 1;\n        for(auto it: adj[node])\n        {\n            if(!vis[it])\n            {\n                dfs(it, adj, vis, cnt);\n            }\n        }\n    }\n    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {\n        int n = graph.size();\n        unordered_map<int, set<int>> adj;\n        prepare(adj, graph);\n\n// Logic: Try to remove each initial malicious node from graph and then call dfs for each other malicious node and count how many infected.(i,e. given by the total nodes reachable from thode rest initial nodes)\n// The one node on whose removal we have the least number of infected nodes is the answer.\n// To remove a node, just at initialization make its visited 1. So all it will never be called by dfs. (Good Trick)\n        int ans = initial[0];\n        int infected = INT_MAX;\n        for(int i = 0 ; i < initial.size() ; i++)\n        {\n            vector<int> vis(n, 0);\n            vis[initial[i]] = 1;\n            int count_infected = 0;\n            for(int j = 0 ; j < initial.size() ; j++)\n            {\n                if(!vis[initial[j]])\n                {\n                    dfs(initial[j], adj, vis, &count_infected);\n                }\n            }\n            if(count_infected < infected)\n            {\n                infected = count_infected;\n                ans = initial[i];\n            }\n            else if(count_infected == infected)\n            {\n                ans = min(ans, initial[i]);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++', 'Java']