leetcode 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 < graph.length = graph[0].length <= 300
0 <= graph[i][j] == graph[j][i] <= 1
graph[i][i] = 1
1 <= initial.length < graph.length
0 <= initial[i] < graph.length

现在有一个网络,由邻接矩阵可知其连通性。在网络中,有几个节点被感染,并且会感染与其相连的所有节点。现在从已知的,最初被感染的节点中去除一个,使网络中被感染的节点达到最小,则应该去除哪个节点。
在做题之前,我们可以先考虑图的组成。对于连通图,也就是一个树,如果存在两个及两个以上的初始感染节点,那么只有将被感染的节点全部去除,才能使这颗树不被感染,否则,这颗树的节点将全部感染。由此,可推的非连通图的情况:如果一个连通量中,存在多个被感染的初始节点,那么无论去除哪个初始感染节点,这个连通量都将被感染。所以,要找寻图中的仅有一个初始感染点且连通量所包含的节点数尽可能大。


class Solution {
private:
   
    void ini(int len,vector<int>& root){
        for(int i=0;i<len;i++)
            root[i]=i;
    }
    int seekRoot(int x,vector<int>& root){
        if(root[x]==x)
            return x;
        else{
            int r=seekRoot(root[x],root);
            return root[x]=r;
        }
    }
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        vector<int> root(324);
        vector<int> num(324,1);
        int len=graph[0].size();
        int wide=graph.size();
        ini(len,root);
        for(int i=0;i<len;i++)
            for(int j=0;j<i;j++)
                if(graph[i][j]){
                    int rootA=seekRoot(i,root);
                    int rootB=seekRoot(j,root);
                    if(rootA!=rootB){
                        root[rootA]=rootB;
                        num[rootB]+=num[rootA];
                    }
                }
        vector<int> org(324,0);
        vector<int> store;
        for(auto ele : initial){
            int iniRoot=seekRoot(ele,root);
            store.push_back(iniRoot);
            org[iniRoot]++;
        }
        int ansIndex=-2;
        for(int i=0;i<initial.size();i++){
            if(org[store[i]]>1)
                continue;
            if(ansIndex==-2)
                ansIndex=i;
            else{
                if(num[store[i]]>num[store[ansIndex]])
                    ansIndex=i;
                else{
                    if(num[store[i]]==num[store[ansIndex]] && initial[i]<initial[ansIndex])
                        ansIndex=i;
                }
            }
        }
        //printf("ansIndex=%d\n",ansIndex);
        if(ansIndex==-2){
            int least=INT_MAX;
            for(int i=0;i<initial.size();i++)
                if(least>initial[i]){
                    least=initial[i];
                    ansIndex=i;
                }
                
        }
        //printf("ansIndex=%d\n",ansIndex);
        return initial[ansIndex];
    }
};

猜你喜欢

转载自blog.csdn.net/white_156/article/details/83090988