<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>

struct Node{

    int idx;
    Node* next;

};

class BFS{

public:

    Node* head;
    Node* tail;
    int n;

    BFS(int n_node){
        n = n_node;
        head = nullptr;
        tail = nullptr;
    }

    void push(int idx){

        Node* newNode = new Node{idx, nullptr};
        if (!head){
            head = newNode;
            tail = newNode;
        } else{
            Node* temp = tail;
            tail = newNode;
            temp->next = newNode;
        }

    }

    int pop(){

        int val = head->idx;
        Node* temp = head;
        head = head->next;

        temp->next = nullptr;
        delete temp;

        return val;

    }

    void doBFS(int adj[300][300], int visit[300], int start, int& decre, int& min_idx, int initial[300]){

        int cnt = 0;
        int cnt_mal = 0;

        min_idx = 0;

        push(start);
        visit[start] = 1;
        cnt+=1;
        cnt_mal+=1;
        min_idx = start;

        while(head){

            int curr = pop();
            for (int i=0;i<n;i++){
                if(i!=curr && adj[i][curr]==1 && visit[i]==0){
                    push(i);
                    visit[i] = 1;
                    cnt+=1;
                    if (initial[i]==1){
                        cnt_mal+=1;
                        if (min_idx > i) min_idx = i;
                    }
                }
            }
        }

        if (cnt_mal>=2){
            decre = 0;
        }else{
            decre = cnt;
        }

    }

};

class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        
        int n = graph.size();

        int max_val = -1;
        int max_val_idx = 99999;

        //
        int visit[300];
        for (int i=0;i<300;i++){
            visit[i] = 0;
        }

        //
        int graph_a[300][300];
        for (int i=0;i<300;i++){
            for (int j=0;j<300;j++){
                graph_a[i][j] = 0;
            }
        }
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                graph_a[i][j] = graph[i][j];
            }
        }

        //
        int initial_a[300];
        for (int i=0;i<300;i++){
            initial_a[i] = 0;
        }
        for (int i=0;i<initial.size();i++){
            initial_a[initial[i]] = 1;
        }

        for (int i=0;i<initial.size();i++){

            int node_idx = initial[i];

            if (visit[node_idx]==0){
                int decre = -1;
                int min_idx = 9999;

                BFS bfs(n);

                bfs.doBFS(graph_a, visit, node_idx, decre, min_idx, initial_a);

                if(decre>max_val){
                    max_val = decre;
                    max_val_idx = min_idx;
                } else if(decre==max_val){
                    if (min_idx < max_val_idx){
                        max_val_idx = min_idx;
                    }
                }


            }
        }
        return max_val_idx;
    }
};
68645