Union Find Based| C++| Approach
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/*\nThe approach utilizes Disjoint Set Union (DSU) to model the connectivity of nodes in the graph, excluding initially infected nodes. It then tracks parent-child relationships between infected nodes and their connected components using freq and freq2 vectors. By calculating the size of DSU components connected to each infected node, it determines which removal minimizes malware spread, considering connections and avoiding redundant infections. Finally, it selects the node with the smallest index among ties to optimize removal and minimize potential malware propagation.\n*/\nclass DSU {\npublic:\n    vector<int> parent, rank, size;\n    \n    DSU(int n) {\n        parent.resize(n);\n        rank.resize(n, 0);\n        size.resize(n, 1);\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    int find(int x) {\n        if (x == parent[x]) {\n            return x;\n        }\n        return parent[x] = find(parent[x]);\n    }\n    \n    void unionbyrank(int x, int y) {\n        int xx = find(x);\n        int yy = find(y);\n        if (xx == yy) return;\n        if (rank[xx] < rank[yy]) {\n            parent[xx] = yy;\n        } else if (rank[xx] > rank[yy]) {\n            parent[yy] = xx;\n        } else {\n            rank[xx]++;\n            parent[yy] = xx;\n        }\n        return;\n    }\n\n    void unionbysize(int x, int y) {\n        int xx = find(x);\n        int yy = find(y);\n        if (xx == yy) return;\n        if (size[xx] < size[yy]) {\n            parent[xx] = yy;\n            size[yy] += size[xx];\n        } else if (size[xx] > size[yy]) {\n            parent[yy] = xx;\n            size[xx] += size[yy];\n        } else {\n            size[xx] += size[yy];\n            parent[yy] = xx;\n        }\n        return;\n    }\n};\n\nclass Solution {\npublic:\n    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {\n        int n = graph.size();\n        DSU ds(n);\n        unordered_map<int, int> mp;\n        for (auto t : initial) {\n            mp[t]++;\n        }\n        for (int i = 0; i < n; i++) {\n            if (mp[i]) continue;\n            for (int j = 0; j < n; j++) {\n                if (i == j || mp[j]) continue;\n                if (graph[i][j] == 1) ds.unionbysize(i, j);\n            }\n        }\n        vector<vector<int>> freq(n); \n        vector<vector<int>> freq2(n); \n        for (auto t : initial) {\n            for (int i = 0; i < n; i++) {\n                if (i == t || mp[i]) continue; \n                if (graph[t][i] == 1) {\n                    int par = ds.find(i);\n                    freq[t].push_back(par); \n                    freq2[par].push_back(t);\n                    \n                }\n            }\n        }\n        int ans = -1e8, mini = 1e8;\n        for (auto t:initial) {\n            unordered_set<int>st(freq[t].begin(),freq[t].end()); \n            int sz=0;  \n            for(auto r:st){\n               unordered_set<int>st2(freq2[r].begin(),freq2[r].end());\n               if(st2.size()>1)continue;\n                sz+=ds.size[r];\n            }\n            if(ans<sz){\n                ans=sz;\n                mini=t;\n            }\n            if(ans==sz){\n                mini=min(t,mini);\n            }\n        }\n        if (ans == -1e8) return *min_element(initial.begin(), initial.end());\n        return mini;\n    }\n};\n```
['C++']