<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:
        #iterate through each node, and if a node is connected to another node, then we join them
        # we want the number of unique node that each node infects

        # we can go through each node, and 

        #get the indegree of infected for each node. 

        #then for each intial, we run an 'unlinkin' of adj nodes which will decrease their indegree by 1. 
        #if indegree is 0 we add it to the nodes

        #run a dfs from each infected node. go through neighs, increase infected degree by 1
        # make sure to have visited set as well to prevent duplicates // might need adj list
        # from each infected ill run a bfs and add all nodes that were only infected cause of original node
        # get the max from all those results
        def infect(node,src):
            if node in visited:
                return
            # infected_degree[node] +=1
            visited.add(node)
            node_to_who_infected[node].append(src)
            for nei in range(n):
                if nei != node and graph[node][nei] == 1:
                    infect(nei,src)
            return

        # def remove_infection(node):
        #     if node in visited:
        #         return 0
        #     visited.add(node)
        #     res = 1
        #     for nei in adj[node]:
        #         if infected_degree[nei] == 1:
        #             res += remove_infection(nei)
        #     return res

        # #create adj list
        # adj = defaultdict(list)
        # for i in range(n):
        #     for j in range(n):
        #         if graph[i][j] == 1:
        #             adj[i].append(j)
        #             adj[j].append(i)
        
        #create a counter for the number of nodes that infect each index
        # infected_degree = [0]*n
        n = len(graph)
        node_to_who_infected = defaultdict(list)
        node_to_unique_infections = defaultdict(int)

        for node in initial:
            visited = set()
            infect(node,node)

        for node,who_infected in node_to_who_infected.items():
            if len(who_infected) == 1:
                node_to_unique_infections[who_infected[0]] +=1
        
        best_node = 0
        max_reduced = -1
        for node in initial:
            cnt = node_to_unique_infections[node]
            if cnt > max_reduced:
                best_node = node
                max_reduced = cnt
            elif cnt == max_reduced and node < best_node:
                best_node = node
        return best_node

        # #for each initally infected node, return the number that are only infected because of it
        # minimize_idx = None
        # max_reduced = -1
        # for node in initial:
        #     visited = set()
        #     reduction = remove_infection(node) if infected_degree[node] == 1 else 0
        #     if reduction > max_reduced:
        #         max_reduced = reduction
        #         minimize_idx = node
        
        # return minimize_idx

            


20200