924. Minimize Malware Spread

2 篇文章 0 订阅
2 篇文章 0 订阅

https://leetcode.com/problems/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

题目大意:在一个邻接矩阵表示的图中,如果graph[ i ][ j ]==1,表示 i 和 j 相连,知道一个被污染点的向量,所有与其中节点相连的都会被感染,现在要求去掉感染最多点的那个节点,如果其中存在两个或多个节点所能感染节点数目相同,返回节点编号较小的那个。

算法思路:很明显采用广搜bfs或者深搜dfs,前两段代码是将感染源去掉某一个点之后的计算剩余点的整体污染个数,存在大量的重复计算,在第三段代码中,采用计算每一个点得可污染节点数目,不过res要现赋初值,initial需要sort一下,因为没有采用数组记录每个点的数目,而是通过visited布尔型数组进行标记是否访问过,如果1和3是initial向量中的点,并且是联通的,如果3先访问,那么1会被动访问,这时候如果1和3是感染最少点数的污点,那么最后答案是3,并不是我们要的1,所以将initial向量sort一下就可以避免这种问题,算法时间复杂度变为O(n^2),计算速度大大加快。

class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int mn=INT_MAX,res=0;
        unordered_set<int> infected(initial.begin(),initial.end());
        for(int x : initial){
            infected.erase(x);
            int cnt=bfs(graph,infected);
            if(cnt<mn || (cnt==mn && res>x)){
                mn=cnt;
                res=x;
            }
            infected.insert(x);
        }
        return res;
    }
private:
    int bfs(vector<vector<int>>& graph, unordered_set<int> infected){
        queue<int> q;
        for(int x : infected){
            q.push(x);
        }
        
        while(!q.empty()){
            int t=q.front();
            q.pop();
            for(int i=0;i<graph[t].size();i++){
                if(graph[t][i]!=1 || infected.count(i)) continue;
                q.push(i);
                infected.insert(i);
            }
        }
        return infected.size();
    }
};
class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int mn=INT_MAX,res=0;
        unordered_set<int> infected(initial.begin(),initial.end());
        for(int x : initial){
            infected.erase(x);
            int cnt=0;
            unordered_set<int> visited;
            for(int cur : infected){
                dfs(graph,visited,cur,cnt);
            }
            
            if(cnt<mn || (cnt==mn && res>x)){
                mn=cnt;
                res=x;
            }
            infected.insert(x);
        }
        return res;
    }
private:
    void dfs(vector<vector<int>>& graph, unordered_set<int>& visited,int cur,int& cnt){
        if(visited.count(cur)) return;
        visited.insert(cur);
        ++cnt;
        for(int i=0;i<graph[cur].size();i++){
            if(graph[cur][i]!=1) continue;
            dfs(graph,visited,i,cnt);
        }
        return;
    }
};
//改造后的dfs,O(n^2)  O(n)
//Runtime: 84 ms, faster than 98.86% of C++ online submissions for Minimize Malware Spread.
//Memory Usage: 27.8 MB, less than 100.00% of C++ online submissions for Minimize Malware Spread.

class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int max_node=0,res=initial[0];
        sort(initial.begin(),initial.end());
        vector<bool> visited(graph.size(),false);
        
        for(int x : initial){
            int cnt=0;
            dfs(graph,visited,x,cnt);
            if(cnt>max_node){
                max_node=cnt;
                res=x;
            }
        }
        return res;
    }
private:
    void dfs(vector<vector<int>>& graph, vector<bool>& visited,int cur,int& cnt){
        if(visited[cur]==true) return;
        visited[cur]=true;
        ++cnt;
        for(int i=0;i<graph[cur].size();i++){
            if(graph[cur][i]!=1) continue;
            dfs(graph,visited,i,cnt);
        }
        return;
    }
};

//如果不加上这段加速IO的代码,时间大概是180ms,说明读入数据占据了很多时间
static const int fastIO = []() {  
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
}();

//还可以通过并查集解决,并查集算法尚未完全熟练,待续。。。

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值