# https://leetcode.com/problems/minimize-malware-spread/
"""
In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.

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

Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.

We will remove one node from the initial list.  Return the node that if removed, would minimize M(initial).  If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.

Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of the malware spread.

Example 1:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:
Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0

Example 3:
Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
Output: 1

Note:
1 < graph.length = graph[0].length <= 300
0 <= graph[i][j] == graph[j][i] <= 1
graph[i][i] == 1
1 <= initial.length <= graph.length
0 <= initial[i] < graph.length
"""
from typing import List


def min_malware_spread(graph: List[List[int]], initial: List[int]) -> int:
    class UF:
        def __init__(self, n):
            self.count = n
            self.parent = list(range(n))

        def find(self, x):
            while self.parent[x] != x:
                self.parent[x] = self.parent[self.parent[x]]
                x = self.parent[x]
            return x

        def union(self, u, v):
            root_u, root_v = self.find(u), self.find(v)
            if root_u == root_v:
                return
            self.parent[root_u] = root_v
            self.count -= 1

    def get_affected(uf, initial, k, n):
        affected = 0
        parents = {uf.find(x) for x in initial if x != k}
        for i in range(n):
            if uf.find(i) in parents:
                affected += 1
        return affected

    n = len(graph)
    uf = UF(n)
    for i in range(n):
        for j in range(i + 1, n):
            if graph[i][j] == 1:
                uf.union(i, j)

    initial.sort()
    res = None
    min_affected = float("inf")
    for k in initial:
        affected = get_affected(uf, initial, k, n)
        if affected < min_affected:
            min_affected = affected
            res = k
    return res


if __name__ == "__main__":
    min_malware_spread(
        [
            [1, 0, 0, 0, 1, 0, 0, 0],
            [0, 1, 1, 0, 0, 1, 0, 0],
            [0, 1, 1, 0, 1, 0, 0, 0],
            [0, 0, 0, 1, 1, 0, 0, 0],
            [1, 0, 1, 1, 1, 0, 0, 1],
            [0, 1, 0, 0, 0, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 1],
            [0, 0, 0, 0, 1, 0, 1, 1],
        ],
        [7, 2],
    )
