<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 {
public:

    void MarkColorDFS(
        const std::vector<std::vector<int> >& graph,
        std::vector<int>& coloredNodes,
        std::unordered_map<int, int>& colorCount,
        int startNode,
        int colorIdx
    )
    {
        std::vector<int> stack(1, startNode);
        while(!stack.empty())
        {
            int currNode = stack.back();
            stack.pop_back();

            if (coloredNodes[currNode] != -1)
            {
                // we have marked it --> skip it
                continue;
            }

            // mark it
            coloredNodes[currNode] = colorIdx;
            ++colorCount[colorIdx];

            // look for the next node
            for (size_t i = 0; i < graph[currNode].size(); ++i)
            {
                if (graph[currNode][i] == 1)
                {
                    stack.push_back(i);
                }
            }
        }
    }

    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial)
    {
        std::vector<int> coloredNodes(graph.size(), -1);
        std::unordered_map<int, int> colorCount;
        int availableColor = 0;
        for (size_t i = 0; i < graph.size(); ++i)
        {
            if (coloredNodes[i] == -1)
            {
                // node i is not colored yet
                MarkColorDFS(graph, coloredNodes, colorCount, i, availableColor++);
            }
        }

        // we want to remove nodes in initial that are having the same color
        for (const auto& init: initial)
        {
            // what is the color of this node?
            int color = coloredNodes[init];
            // how many nodes in total are in this color
            auto it = colorCount.find(color);
            if (it == colorCount.end())
            {
                // color count doesn't have this color,
                // meaning that we have kicked it out
                // due to same color in initial
            }
            else if (it->second < 0)
            {
                // the count is negative, meaning that we met it before
                // so kick it out
                colorCount.erase(it);
            }
            else
            {
                // it's our first time meeting this color
                it->second *= -1;
            }
        }

        // now it's time to find out the max initial node
        int maxInitial = -1;
        int maxCount = 0;
        for (const auto& init: initial)
        {
            // what is the color of this node?
            int color = coloredNodes[init];
            // how many nodes in total are in this color
            auto it = colorCount.find(color);
            if (it == colorCount.end())
            {
                // this color has been kicked out
            }
            else if (-(it->second) > maxCount)
            {
                // There are more nodes in this color
                maxInitial = init;
                // NOTE: we flip the counter into a negative number
                // in the previous step
                maxCount = -(it->second);
            }
            else if (
                (-(it->second) == maxCount) &&
                (init < maxInitial)
            )
            {
                // The counts are equal, but this node has smaller index
                maxInitial = init;
                maxCount = -(it->second);
            }
        }

        if (maxInitial == -1)
        {
            // there is no answer -> return the min(initial)
            auto it = std::min_element(initial.begin(), initial.end());
            maxInitial = *it;
        }

        return maxInitial;
    }
};
70335