并查集的代码

省份数量

class Solution {
private:
    int n;
    vector<int> father;
    void init(){
        for(int i=0;i<n;i++){
            father.push_back(i);
        }
    }
    int find(int u){
        if(u==father[u])
            return u;
        else
            father[u]=find(father[u]);
            return father[u];
    }
    void join(int v,int u){
        u=find(u);
        v=find(v);
        if(v==u)
            return;
        father[u]=v;
    }
public:
    int findCircleNum(vector<vector<int>>& isConnected) {
        n=isConnected.size();
        init();
        for(int i=0;i<n;++i)
            for(int j=0;j<n;j++)
                if(isConnected[i][j])
                    join(i,j);
        
        unordered_set<int> s;
        for(int i=0;i<n;++i)
            s.insert(find(i));
        return s.size();
    }
};

冗余连接

class Solution {
private:
    int n=1005;
    vector<int> father;
    void init(){
        for(int i=0;i<n;i++){
            father.push_back(i);
        }
    }
    int find(int u){
        if(u==father[u])
            return u;
        else
            father[u]=find(father[u]);
            return father[u];
    }
    void join(int v,int u){
        u=find(u);
        v=find(v);
        if(v==u)
            return;
        father[u]=v;
    }
    bool same(int v,int u){
        v=find(v);
        u=find(u);
        return v==u;
    }
public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        init();
        for(vector<int>& edge:edges){
            if(same(edge[0],edge[1]))
                return edge;
            else
                join(edge[0],edge[1]);
        }
        return {};
    }
};

冗余连接2

class Solution {
private:
    static const int N = 1010;
    int n;
    vector<int> father;
    void init(){
        for(int i=0;i<N;i++){
            father.push_back(i);
        }
    }
    int find(int u){
        if(u==father[u])
            return u;
        else
            father[u]=find(father[u]);
            return father[u];
    }
    void join(int v,int u){
        u=find(u);
        v=find(v);
        if(v==u)
            return;
        father[u]=v;
    }
    bool same(int v,int u){
        v=find(v);
        u=find(u);
        return v==u;
    }
    
    vector<int> getRemoveEdge(vector<vector<int>>& edges) {
        init();
        for(vector<int>& edge:edges){
            if(same(edge[0],edge[1]))
                return edge;
            else
                join(edge[0],edge[1]);
        }
        return {};
    }
        // 删一条边之后判断是不是树,deleteEdge对应的是在列表中的索引
    bool isTreeAfterRemoveEdge(const vector<vector<int>>& edges, int deleteEdge) {
        init(); // 初始化并查集
        for (int i = 0; i < n; i++) {
            if (i == deleteEdge) continue;
            if (same(edges[i][0], edges[i][1])) { // 构成有向环了,一定不是树
                return false;
            }
            join(edges[i][0], edges[i][1]);
        }
        return true;
    }
public:
    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
        n=edges.size();
        vector<int> inDegree(N);
        for(vector<int>& edge:edges){
            inDegree[edge[1]]++;
        }
        vector<int> vec;
         // 找入度为2的节点所对应的边,注意要倒叙,因为优先返回最后出现在二维数组中的答案
        for (int i = n - 1; i >= 0; i--) {
            if (inDegree[edges[i][1]] == 2) {
                vec.push_back(i);
            }
        }
        if(vec.size()>0){
            if(isTreeAfterRemoveEdge(edges,vec[0])) 
                return edges[vec[0]];
            else
                return edges[vec[1]];
        }
            
        return getRemoveEdge(edges);
    }
};

你可能感兴趣的:(图,算法,c++,算法,图搜索算法)