<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 Solution:
    def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
        nn = len(graph)
        parents = list(range(nn))
        ranks = [1]*nn
        iset = set(initial)
        
        def find(xx):
            if parents[xx]!=xx:
                parents[xx] = find(parents[parents[xx]])
            return parents[xx]
        
        def union(aa,bb):
            pa,pb = find(aa), find(bb)

            if pa==pb: return False

            if ranks[pa]>=ranks[pb]:
                parents[pb] = pa
                ranks[pa] += ranks[pb]
            else:
                parents[pa] = pb
                ranks[pb] += ranks[pa]
            return True

        uninfected = list(set(list(range(nn))) - set(initial))

        for ii in range(len(uninfected)):
            for jj in range(ii+1,len(uninfected)):
                if graph[uninfected[ii]][uninfected[jj]]:
                    union(uninfected[ii],uninfected[jj])
        
        # print(parents,rank)

        infected_parents = defaultdict(set)
        infection_ct = defaultdict(int)
        cmax = [-1,-float('inf')]

        # now count infections for each parents
        for ii in initial:
            for jj in uninfected:
                if graph[ii][jj]:
                    infected_parents[ii].add(find(jj))
            
            for pp in infected_parents[ii]:
                infection_ct[pp] += 1

        # now count all the uninfected clusters after pruning each infection
        for ii in initial:
            ct = 0
            for pp in infected_parents[ii]:
                if infection_ct[pp]==1:
                    ct += ranks[pp]
            cmax = max(cmax, [ct,-ii])

        return -cmax[1]
19300