当前位置:网站首页>leetcode684. Redundant connection (medium)

leetcode684. Redundant connection (medium)

2022-06-11 16:15:00 Heavy garbage

 Insert picture description here
 Insert picture description here
 Insert picture description here
Ideas : Union checking set
When connectivity is found , Is the last edge ( Because this edge is the last edge of the ring , So when connectivity is found, it is ans

class Solution {
    
public:
    int father[1005];
    void unite(int u, int v) {
    
        int fau = findfather(u); 
        int fav = findfather(v);
        if(fau != fav) father[fau] = fav;
    }
    int findfather(int m) {
    
        if (father[m] == m) return m;
        return father[m] = findfather(father[m]); 
    }
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
    
        for (int i = 1; i < 1005; ++i) {
    
            father[i] = i;
        }
        for (auto& a : edges) {
    
            int first = a[0], second = a[1];
            if (findfather(first) == findfather(second)) return a;
            unite(a[0], a[1]);
        }
        return {
    };
    }
};
原网站

版权声明
本文为[Heavy garbage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111601470616.html