class Solution:
    """
    @param graph: the node graph
    @param initial: the infected node
    @return: the node index
    """
    def minMalwareSpread(self, graph, initial):
        # write your code here.
        n = len(graph)
        parent = [i for i in range(n)]
        area = [1] * n
        def findParent(i):
            while parent[i] != i:
                parent[i] = parent[parent[i]]
                i = parent[i]
            return i

        for i in range(n):
            for j in range(i + 1, n):
                if graph[i][j]:
                    p1 = findParent(i)
                    p2 = findParent(j)
                    if p1 != p2:
                        parent[p1] = p2
                        area[p2] += area[p1]
                        
        malware = [0] * n
        minIndex = n
        for i in initial:
            minIndex = min(minIndex, i)
            malware[findParent(i)] += 1
        maxArea = 0
        for i in initial:
            p = findParent(i)
            if malware[p] == 1:
                if area[p] > maxArea:
                    maxArea = area[p]
                    minIndex = i
                elif area[p] == maxArea:
                    minIndex = min(minIndex, i)
        return minIndex
