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

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

 

posted @ 2018-10-16 21:21  Asurudo  阅读(488)  评论(0编辑  收藏  举报