leetcode 684 冗余连接

简介: leetcode 684 冗余连接

冗余连接


b47dc33c0a254a6d82e1806d1ab1e560.png

class Solution {
public:
    int n = 0;
    int find(int u , vector<int> &father)
    {
        if(u == father[u]) return father[u];
        father[u] = find(father[u] , father);
        return father[u];
    }
    void join(int u , int v , vector<int> &father)
    {
        u = find(u , father);
        v = find(v , father);
        if(u == v) return;
        father[v] = u;
    }
    bool same(int u , int v , vector<int> &father)
    {
        u = find(u , father);
        v = find(v , father);
        return u == v;
    }
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        n = edges.size() + 1;
        vector<int> father(n,0);
        for(int i=0 ; i<n ; i++)
            father[i] = i;
        for(int i=0 ; i<edges.size() ; i++)
        {
            if(same(edges[i][0] , edges[i][1] , father)) return edges[i];
            else join(edges[i][0] , edges[i][1] , father);
        }
        return {};
    }
};
相关文章
|
12月前
|
算法 C++ Python
每日算法系列【LeetCode 685】冗余连接 II
每日算法系列【LeetCode 685】冗余连接 II
|
12月前
|
算法 C++ Python
每日算法系列【LeetCode 684】冗余连接
每日算法系列【LeetCode 684】冗余连接
leetcode 685 冗余连接II
leetcode 685 冗余连接II
70 0
leetcode 685 冗余连接II
LeetCode每日一题——1640. 能否连接形成数组
如果可以连接 pieces 中的数组形成 arr ,返回 true ;否则,返回 false 。
57 0
|
算法
【Day30】LeetCode算法 [769. 最多能完成排序的块 ] [2131. 连接两字母单词得到的最长回文串]
学习LeetCode算法 [769. 最多能完成排序的块 ] [2131. 连接两字母单词得到的最长回文串]。
93 0
【Day30】LeetCode算法 [769. 最多能完成排序的块 ] [2131. 连接两字母单词得到的最长回文串]
Leetcode | 连接两字母单词得到的最长回文串
Leetcode | 连接两字母单词得到的最长回文串
109 0
Leetcode | 连接两字母单词得到的最长回文串
【day09】LeetCode(力扣)每日一刷[1640. 能否连接形成数组 ][102. 二叉树的层序遍历 ][704. 二分查找 ]
学习了解[1640. 能否连接形成数组 ][102. 二叉树的层序遍历 ][704. 二分查找 ]。
75 0
【day09】LeetCode(力扣)每日一刷[1640. 能否连接形成数组 ][102. 二叉树的层序遍历 ][704. 二分查找 ]
|
机器学习/深度学习 自然语言处理 算法
每日算法系列【LeetCode 685】冗余连接 II
在本问题中,有根树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。每一个节点只有一个父节点,除了根节点没有父节点。
100 0
每日算法系列【LeetCode 685】冗余连接 II
|
算法 容器
[leetcode] 连接所有点的最小费用 -MST
这道题是最小生成树板子题 可以用并查集实现,贪心排序边权 讲一个二元组放在一个vector容器里面,其中的元素为<weight,<u,v>>对应<int,<int,int> >类型,第一个参数代表边权的大小,后面的为两个点u,v,然后按照第一个值边权从小到大排序,然后用并查集实现是否连通,从而实现最小生成树 代码有点套娃(
84 0
[leetcode] 连接所有点的最小费用 -MST
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0