天天看點

LeetCode周賽#106 Q4 Minimize Malware Spread (bfs)

題目來源:https://leetcode.com/contest/weekly-contest-106/problems/minimize-malware-spread/

問題描述

924. Minimize Malware Spread

In a network of nodes, each node 

i

 is directly connected to another node 

j

 if and only if 

graph[i][j] = 1

.

Some nodes 

initial

 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.

Suppose 

M(initial)

 is the final number of nodes infected with malware in the entire network, after the spread of malware stops.

We will remove one node from the initial list.  Return the node that if removed, would minimize 

M(initial)

.  If multiple nodes could be removed to minimize 

M(initial)

, return such a node with the smallest index.

Note that if a node was removed from the 

initial

 list of infected nodes, it may still be infected later as a result of the malware spread.

Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]      
Output: 0      

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]      
Output: 0      

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]      
Output: 1      

Note:

  1. 1 < graph.length = graph[0].length <= 300

  2. 0 <= graph[i][j] == graph[j][i] <= 1

  3. graph[i][i] = 1

  4. 1 <= initial.length < graph.length

  5. 0 <= initial[i] < graph.length

------------------------------------------------------------

題意

給定一個圖的鄰接矩陣和初始點集的數組,問從初始點集中去掉哪個點,最終從初始點集派生出的最大連通分量包含的點數最小。如果有多個點作用相同,則輸出索引最小的點。

------------------------------------------------------------

思路

周遊去除初始點集中的每一個點,分别求最大連通分量(BFS),取最小值。這題不配LeetCode Hard難度。

------------------------------------------------------------

代碼

class Solution {
public:
    int bfs(vector<vector<int>>& graph, vector<int>& initial, int i)
    {
        int j, len = initial.size(), a, ret = 0, n = graph.size();
        bool vis[305] = {};
        queue<int> q;
        for (j=0; j<len; j++)
        {
            if (j != i)
            {
                q.push(initial[j]);
                vis[initial[j]] = 1;
                ret++;
            }
        }
        while (!q.empty())
        {
            a = q.front();
            q.pop();
            for (j=0; j<n; j++)
            {
                if (graph[a][j] && !vis[j])
                {
                    q.push(j);
                    vis[j] = 1;
                    ret++;
                }
            }
        }
        return ret;
    }
    
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int i, len = initial.size(), minv = 0x3f3f3f3f, ans, id;
        sort(initial.begin(), initial.end());
        for (i=0; i<len; i++)
        {
            ans = bfs(graph, initial, i);
            if (ans < minv)
            {
                minv = ans;
                id = initial[i];
            }
        }
        return id;
    }
};