<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.</p>

<p>We will remove <strong>exactly one node</strong> from <code>initial</code>, <strong>completely removing it and any connections from this node to any other node</strong>.</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>&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,1,0],[1,1,1],[0,1,1]], initial = [0,1]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
<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;&nbsp;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 {
public:
    // Member variables for the graph and initial infected nodes
    vector<vector<int>> g;
    vector<int> init;
    
    // Main function to minimize malware spread
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        g = graph;
        int ans = 0, largest = INT_MIN;
        
        // Sort the initial infected list to ensure we return the smallest index in case of ties
        sort(initial.begin(), initial.end());
        init = initial;
        
        // Try removing each node from the initial list, one at a time
        for (int i = 0; i < init.size(); ++i) {
            int prev = init[i];  // Store the current node to remove
            init[i] = -1;        // Mark the node as "removed"
            int x = bfs(prev);   // Perform BFS, ignoring the removed node and its connections
            if (x > largest) {   // Update the result if we get a better solution
                largest = x;
                ans = prev;
            }
            init[i] = prev;      // Restore the node back to the initial list
        }
        return ans;       
    }
    
    // BFS function that ignores the removed node and its edges
    int bfs(int removed) {
        queue<int> q;
        vector<bool> visited(g.size(), false);
        
        // Add all initial infected nodes to the queue, except the removed node
        for (auto& n : init) {
            if (n != -1 && n != removed) {
                visited[n] = true;
                q.push(n);
            }
        }
        
        // Perform BFS, ignoring edges leading to the removed node
        while (!q.empty()) {
            int f = q.front();
            q.pop();
            for (int i = 0; i < g[f].size(); ++i) {
                if (g[f][i] && !visited[i] && i != removed) {  // Ignore edges to the removed node
                    visited[i] = true;
                    q.push(i);
                }
            }
        }
        
        // Count the number of uninfected nodes
        int uninfected = 0;
        for (auto b : visited) uninfected += !b;
        return uninfected;
    }
};

50500