<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>

class DisjointSet{
public:
    vector<int> rank,parent,size;

    DisjointSet(int n){
        rank.resize(n+1,0);
        parent.resize(n+1);
        size.resize(n+1,1);
        for(int i=0; i<=n; i++) parent[i]=i;
    }

    int findUltParent(int node){
        if(node==parent[node]) return node;

        return parent[node] = findUltParent(parent[node]); //Path Compression
    }

    void UnionByRank(int u, int v){
        int ultP_u = findUltParent(u);
        int ultP_v = findUltParent(v);

        if(ultP_u == ultP_v) return;

        if(rank[ultP_v]<rank[ultP_u]){
            parent[ultP_v] = ultP_u;
        }else if(rank[ultP_u]<rank[ultP_v]){
            parent[ultP_u] = ultP_v;
        }else{
            parent[ultP_v] = ultP_u;
            rank[ultP_u]++;
        }
    }

    void UnionBySize(int u, int v){
        int ultP_u = findUltParent(u);
        int ultP_v = findUltParent(v);

        if(ultP_u == ultP_v) return;

        if(size[ultP_u]<size[ultP_v]){
            parent[ultP_u] = ultP_v;
            size[ultP_v] += size[ultP_u];
        }else{
            parent[ultP_v] = ultP_u;
            size[ultP_u] += size[ultP_v];
        }
    }
};

class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int n = graph.size();
        int ans = 1e9, minSize = n+1;
        for(auto ini: initial){ //For each value of initial we will create a DSU and neglect this initial value
            DisjointSet ds(n);
            for(int i=0; i<n; i++){
                if(i==ini) continue; //neglected
                for(int j=0; j<n; j++){
                    if(j==ini) continue; //neglected
                    if(i==j) continue;
                    if(graph[i][j]){
                        ds.UnionBySize(i,j);
                    }
                }
            }

            int size = 0;
            set<int>st;
            for(auto it:initial){
                if(it==ini) continue; //neglected
                int parent=ds.findUltParent(it); //Find the corresponding component
                if(st.find(parent)!=st.end()) continue; //this component has already been visited
                st.insert(parent);
                size+=ds.size[parent]; //These many nodes are infected
            }
    
            if(minSize>size){
                ans = ini;
                minSize = size;
            }
            else if(size==minSize) ans=min(ans, ini);
        }
        return ans;
    }
};
45900