DSU|| C++|| MALWARE I EXTENSION || SET
\n# Code\n\n// we will make dsu of only uninfected component after that we will connect the infected node to dsu and \n// check for the size that it affects , n
\n# Code\n```\n// we will make dsu of only uninfected component after that we will connect the infected node to dsu and \n// check for the size that it affects , note if dsu component that is connected to the current infected node \n// if connected with other infected node than it will not contribute to the size due to which I had used the unordered set \n// for the parent of dsu as edge to same component can be more than one  giving redundant answer\n//  same goes with the parent of dsu checking only connected to current infected node \n//  agar 2 infected node aarhi hai component pe to ye to contribute hi nahi karega dead hai\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\n```
['Union Find', 'Ordered Set', 'C++']