<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. We will remove <strong>exactly one node</strong> from <code>initial</code>.</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>Note that if a node was removed from the <code>initial</code> list of infected nodes, it might still be infected later due to the malware spread.</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,0,0],[0,1,0],[0,0,1]], initial = [0,2]
<strong>Output:</strong> 0
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
<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;= 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:
        
        
        # =>  find 连通分量里只有一个初始感染点的，统计每个这种连通分量的大小，取最大的那个   (如果某个 union里有多个 initial感染点，去掉一个没有用)
        # 标记每一块 union的 index,统计大小和里面initial感染点个数
        n = len(graph)
        
        u = UnionFind(n)
        for i in range(n):
            for j in range(n):
                if i == j:     # i == j 不要放进去
                    continue
                    
                if graph[i][j] == 1:
                    u.union(i, j)
        
        headNode = {}
        initialNode = set(y for y in initial)
        
        for x in range(n):  # iterate through all nodes
            head = u.find(x)
            if head not in headNode:
                headNode[head] = [1, 0, 0]
                # (1)component size  (2)initial infact nodes (3) infact node idx
            else:
                headNode[head][0] += 1
            
            if x in initialNode:
                headNode[head][1] += 1
                
                if headNode[head][1] == 1:
                    headNode[head][2] = x
        
        
        # 全联通  /  感染点数为1 + 点数为 multiple
        
        total, resIdx = 0, n-1
        unique = 0
        
        # print(headNode)
        for size, infact, idx in headNode.values():
            if infact == 1:
                unique += 1
                
                if size > total:
                    total = size
                    resIdx = idx
                
                elif size == total and idx < resIdx:
                    resIdx = idx
        
        # print()
        
        if unique > 0:
            return resIdx
        
        return min(initial) #所有块 点数 >= 2
        
        #多个点 移除哪个都没用
            
#         0 - 2
        
#         1   3

#     0    1      2 - 3
        
        
        
class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n)]
    
    
    def union(self, x, y):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX == rootY:
            return
        
        self.parent[rootX] = rootY
        
    
    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        
        return self.parent[x]
        
        
        
        
        
19000