<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 {
    // Perform DFS to traverse the connected nodes in the graph
    void dfs(int source, vector<vector<int>>& graph, unordered_map<int,vector<int>>& influenceMap, int node, vector<int>& visited, int n) {
        // Mark the current node as visited
        visited[node] = 1;
        
        // Explore all nodes connected to the current node
        for (int i = 0; i < n; ++i) {
            if (graph[node][i] == 1 && !visited[i]) {
                // Record the influence of the source node over this node
                influenceMap[i].push_back(source);
                
                // Continue DFS from the connected node
                dfs(source, graph, influenceMap, i, visited, n);
            }
        }
    }

public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int n = graph.size();  // Number of nodes in the graph
        unordered_map<int, vector<int>> influenceMap;  // Map to track which nodes are influenced by which initial infected nodes

        // Perform DFS for each initially infected node
        for (int infectedNode : initial) {
            vector<int> visited(n, 0);  // Track visited nodes during DFS
            influenceMap[infectedNode].push_back(infectedNode);  // Each node influences itself initially
            dfs(infectedNode, graph, influenceMap, infectedNode, visited, n);
        }

        vector<int> influenceCount(n, 0);  // Track the number of nodes uniquely influenced by each initial infected node

        // Iterate over the influence map and count how many nodes are uniquely influenced by a single infected node
        for (const auto& entry : influenceMap) {
            if (entry.second.size() == 1) {
                influenceCount[entry.second[0]]++;  // Only increment for nodes influenced by exactly one infected node
            }
        }

        // Sort the initial infected nodes to ensure we return the smallest index in case of a tie
        sort(initial.begin(), initial.end());

        // Find the node with the maximum unique influence count
        int answer = initial[0];
        for (int i = 1; i < initial.size(); ++i) {
            if (influenceCount[answer] < influenceCount[initial[i]]) {
                answer = initial[i];
            }
        }

        return answer;
    }
};

71245