<p>You are given a network of <code>n</code> nodes represented as an <code>n x n</code> adjacency matrix <code>graph</code>, where the <code>i<sup>th</sup></code> node is directly connected to the <code>j<sup>th</sup></code> node if <code>graph[i][j] == 1</code>.</p>

<p>Some nodes <code>initial</code> 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.</p>

<p>Suppose <code>M(initial)</code> is the final number of nodes infected with malware in the entire network after the spread of malware stops.</p>

<p>We will remove <strong>exactly one node</strong> from <code>initial</code>, <strong>completely removing it and any connections from this node to any other node</strong>.</p>

<p>Return the node that, if removed, would minimize <code>M(initial)</code>. If multiple nodes could be removed to minimize <code>M(initial)</code>, return such a node with <strong>the smallest index</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
<strong>Output:</strong> 0
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
<strong>Output:</strong> 1
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == graph.length</code></li>
	<li><code>n == graph[i].length</code></li>
	<li><code>2 &lt;= n &lt;= 300</code></li>
	<li><code>graph[i][j]</code> is <code>0</code> or <code>1</code>.</li>
	<li><code>graph[i][j] == graph[j][i]</code></li>
	<li><code>graph[i][i] == 1</code></li>
	<li><code>1 &lt;= initial.length &lt;&nbsp;n</code></li>
	<li><code>0 &lt;= initial[i] &lt;= n - 1</code></li>
	<li>All the integers in <code>initial</code> are <strong>unique</strong>.</li>
</ul>

/*
The 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.
*/
class DSU {
public:
    vector<int> parent, rank, size;
    
    DSU(int n) {
        parent.resize(n);
        rank.resize(n, 0);
        size.resize(n, 1);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
    }
    
    int find(int x) {
        if (x == parent[x]) {
            return x;
        }
        return parent[x] = find(parent[x]);
    }
    
    void unionbyrank(int x, int y) {
        int xx = find(x);
        int yy = find(y);
        if (xx == yy) return;
        if (rank[xx] < rank[yy]) {
            parent[xx] = yy;
        } else if (rank[xx] > rank[yy]) {
            parent[yy] = xx;
        } else {
            rank[xx]++;
            parent[yy] = xx;
        }
        return;
    }

    void unionbysize(int x, int y) {
        int xx = find(x);
        int yy = find(y);
        if (xx == yy) return;
        if (size[xx] < size[yy]) {
            parent[xx] = yy;
            size[yy] += size[xx];
        } else if (size[xx] > size[yy]) {
            parent[yy] = xx;
            size[xx] += size[yy];
        } else {
            size[xx] += size[yy];
            parent[yy] = xx;
        }
        return;
    }
};

class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int n = graph.size();
        DSU ds(n);
        unordered_map<int, int> mp;
        for (auto t : initial) {
            mp[t]++;
        }
        for (int i = 0; i < n; i++) {
            if (mp[i]) continue;
            for (int j = 0; j < n; j++) {
                if (i == j || mp[j]) continue;
                if (graph[i][j] == 1) ds.unionbysize(i, j);
            }
        }
        vector<vector<int>> freq(n); 
        vector<vector<int>> freq2(n); 
        for (auto t : initial) {
            for (int i = 0; i < n; i++) {
                if (i == t || mp[i]) continue; 
                if (graph[t][i] == 1) {
                    int par = ds.find(i);
                    freq[t].push_back(par); 
                    freq2[par].push_back(t);
                    
                }
            }
        }
        int ans = -1e8, mini = 1e8;
        for (auto t:initial) {
            unordered_set<int>st(freq[t].begin(),freq[t].end()); 
            int sz=0;  
            for(auto r:st){
               unordered_set<int>st2(freq2[r].begin(),freq2[r].end());
               if(st2.size()>1)continue;
                sz+=ds.size[r];
            }
            if(ans<sz){
                ans=sz;
                mini=t;
            }
            if(ans==sz){
                mini=min(t,mini);
            }
        }
        if (ans == -1e8) return *min_element(initial.begin(), initial.end());
        return mini;
    }
};
46900