美文网首页
924. Minimize Malware Spread

924. Minimize Malware Spread

作者: 想学会飞行的阿番 | 来源:发表于2018-10-15 14:23 被阅读74次

    方法一:并查集

    class Solution {
    public:
        int parents[301];
        int col[301];
        int findparent(int x){
            int p = x,temp = x;
            while(p!=parents[p]){
               p = parents[p];
            }
            while(temp != p){
                int j = parents[temp];
                parents[temp] = p;
                temp = j;
            }
            return parents[x];
        }
        void unit(int x,int y){
            int px = findparent(x);
            int py = findparent(y);
            if(px<py){
                swap(x,y);
                swap(px,py);
            }
            if(px != py){
                parents[px] = py;
                col[py] +=col[px];
            }
            
        }
        int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
            int len = graph.size();
            for(int i =0;i<len;i++) {
                parents[i] = i;
                col[i] = 1;
            }
            for(int i =0;i<len;i++){
                for(int j = i+1;j<len;j++){
                    if(graph[i][j] == 1)
                        unit(i,j);
                }
            }
            sort(initial.begin(),initial.end());
            int maxindex = -1;
            int maxcol = -1;
            for(auto index : initial){
                if(col[findparent(index)]>maxcol)
                {
                    maxcol = col[findparent(index)];
                    maxindex = index;
                }
            }
            return maxindex;
        }
    };
    

    方法二:BFS

    class Solution {
    public:
        int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
            sort(initial.begin(),initial.end());
            vector<int> nodes(graph.size(),1);
            vector<int> temp;
            queue<int> stacks;
            int maxv = INT_MIN;
            int maxi = INT_MIN;
            for(int i =0;i<initial.size();i++){
                if(nodes[initial[i]]!=-1){
                    temp.clear();
                    stacks.push(initial[i]);
                    int index;
                    while(!stacks.empty()){
                        index = stacks.front();
                        nodes[index] = -1;
                        temp.push_back(index);
                        stacks.pop();
                        for(int j = 0;j<graph[index].size();j++){
                            if(nodes[j]==1&&graph[index][j] == 1)
                            {   stacks.push(j);
                                nodes[j] = -1;
                            }
                        }
                    }
                    int c = temp.size();
                    if(c > maxv) {
                        maxv = temp.size();
                        maxi = initial[i];
                    }
                }
            }
            return maxi;
        }
    };
    

    问题:
    🐒奇怪,如果直接temp.size()>maxv,输出为INT_MIN,想不明白

    相关文章

      网友评论

          本文标题:924. Minimize Malware Spread

          本文链接:https://www.haomeiwen.com/subject/mrefzftx.html