<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:
    // Graph representation
    vector<vector<int>> g;

    // Depth-first search (DFS) to count nodes in the connected component excluding a specific node
    int dfs(int nn, int nottotake, vector<int>& vis) {
        vis[nn] = 1; // Mark the current node as visited
        int sum = 1; // Initialize count for the current node
        // Traverse all adjacent nodes
        for (auto adj : g[nn]) {
            // If the node is not visited and is not the node to exclude, continue DFS
            if (!vis[adj] && adj != nottotake) {
                sum += dfs(adj, nottotake, vis); // Add the count of connected nodes
            }
        }
        return sum; // Return the total count of nodes in this connected component
    }

    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int n = graph.size(); // Number of nodes in the graph
        g.resize(n); // Resize the graph to fit the number of nodes

        // Build the adjacency list from the adjacency matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (graph[i][j]) {
                    g[i].push_back(j); // Add an edge between node i and node j
                }
            }
        }

        unordered_map<int, int> mp; // Map to count occurrences of initially infected nodes
        for (auto i : initial)
            mp[i]++; // Increment the count for each initially infected node

        int mini = 1e8, ans; // Initialize minimum infected count and answer node

        // Iterate over each initially infected node to simulate removal
        for (auto ele : initial) {
            int cnt = 1; // Initialize infected count
            vector<int> vis(n, 0); // Visited array to keep track of visited nodes

            // Count the number of nodes that would be infected if 'ele' is removed
            for (int i = 0; i < n; i++) {
                if (!vis[i] && mp[i] && i != ele)
                    cnt += dfs(i, ele, vis); // Perform DFS to count infected nodes
            }

            // Update the answer if the current count is less than the minimum found so far
            if (cnt < mini) {
                mini = cnt;
                ans = ele;
            }
            // If the count is the same, choose the node with the smaller index
            else if (cnt == mini) {
                ans = min(ans, ele);
            }
        }

        return ans; // Return the node that should be removed to minimize the malware spread
    }
};
47500