Leetcode-928 Minimize Malware Spread II(尽量减少恶意软件的传播 II)

 1 class Solution
 2 {
 3     private:
 4         int visited[309];
 5         int nowKilled;
 6         int numOfNode;
 7         int toBeDeleted;
 8     public:
 9         void DFS(int killer,vector<vector<int>> &graph)
10         {
11             visited[killer] = 1;
12             nowKilled ++;
13             for(int i = 0; i < numOfNode; i ++)
14             {
15                 if(graph[killer][i] && !visited[i])
16                 {
17                     DFS(i,graph);
18                 }
19             }
20         }
21         int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial)
22         {
23             memset(visited,0,sizeof(visited));
24             numOfNode = graph.size();
25             int nowLeastKilled = numOfNode+1,result;
26             vector<int> killerList(numOfNode,0);
27             for(auto d:initial)
28                 killerList[d] = 1;
29             sort(initial.begin(),initial.end());
30             for(auto killer:initial)
31             {
32                 toBeDeleted = killer;
33                 for(int k = 0; k < numOfNode; k ++)
34                 {
35                     visited[k] = 0;
36                 }
37                 nowKilled = 0;
38 
39                 for(int k = 0; k < numOfNode; k ++)
40                 {
41                     if(k != toBeDeleted && !visited[k] && killerList[k])
42                     {
43                         DFS(k,graph);
44                     }
45                 }
46                 if(nowKilled < nowLeastKilled)
47                 {
48                     nowLeastKilled = nowKilled;
49                     result = toBeDeleted;
50                 }
51 
52             }
53             return result;
54         }
55 };

 

转载于:https://www.cnblogs.com/Asurudo/p/9838971.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值