首页 > 其他分享 >【并查集】冗余连接

【并查集】冗余连接

时间:2024-05-14 16:20:22浏览次数:12  
标签:index vector parent int 查集 find edges 连接 冗余

冗余连接

如果两个顶点属于相同的连通分量,则说明在遍历到当前的边之前,这两个顶点之间已经连通,因此当前的边导致环出现,为附加的边,将当前的边作为答案返回
Python

class Solution:
    def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
        n = len(edges)
        parent = list(range(n + 1))

        def find(index: int) -> int:
            if parent[index] != index:
                parent[index] = find(parent[index])
            return parent[index]

        def union(index1: int, index2: int):
            parent[find(index1)] = find(index2)

        for node1, node2 in edges:
            if find(node1) != find(node2):
                union(node1, node2)
            else:
                return [node1, node2]

        return []

C++

class Solution {
public:
    int Find(vector<int>& parent, int index) {
        if (parent[index] != index) {
            parent[index] = Find(parent, parent[index]);
        }
        return parent[index];
    }

    void Union(vector<int>& parent, int index1, int index2) {
        parent[Find(parent, index1)] = Find(parent, index2);
    }

    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        int n = edges.size();
        vector<int> parent(n + 1);
        for (int i = 1; i <= n; ++i) {
            parent[i] = i;
        }
        for (auto& edge: edges) {
            int node1 = edge[0], node2 = edge[1];
            if (Find(parent, node1) != Find(parent, node2)) {
                Union(parent, node1, node2);
            } else {
                return edge;
            }
        }
        return vector<int>{};
    }
};

作者:力扣官方题解
链接:https://leetcode.cn/problems/redundant-connection/solutions/557616/rong-yu-lian-jie-by-leetcode-solution-pks2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签:index,vector,parent,int,查集,find,edges,连接,冗余
From: https://www.cnblogs.com/peterzh/p/18191556

相关文章

  • 【图的连通性】【并查集】【拓扑排序】
    在图论中,不同类型的图(无向图和有向图)需要使用不同的算法和数据结构来处理它们的特性和问题。这里我们将讨论如何使用并查集来解决无向图的连通性问题,以及如何使用深度优先搜索(DFS)、广度优先搜索(BFS)和拓扑排序来解决有向图中的依赖性问题。无向图的连通性:并查集对于无向图的连通......
  • 安装mysql和运程连接
    一、拉取镜像#拉取镜像dockerpullmysql#或者dockerpullmysql:latest#指定版本号dockerpullmysql:5.7#以上两个命令是一致的,默认拉取的就是latest版本的#我们还可以用下面的命令来查看可用版本:dockersearchmysql二、查看镜像dockerimages三、运......
  • 连接查询(多表查询)
    连接两个条件where...and...非等值连接自连接......
  • 连接SQL Server报错
    将框架从.NET6升级到8,顺便将各种依赖包也升级,容器化部署到测试环境后,SQLServer连接不了了:[2024-05-1313:48:10ERR][Microsoft.EntityFrameworkCore.Database.Connection]Anerroroccurredusingtheconnectiontodatabase'Demo'onserver'127.0.0.1'.[2024-05-131......
  • 连接mysql异常
    问题描述C#连接MySql时,System.Security.Authentication.AuthenticationException:调用SSPI失败,请参见内部异常。所用版本4.5.0原因分析:据查此问题因mysql数据库没有安装ssl证书导致。解决方案:连接字符串中加上“SslMode=none”,。stringconnectStr="server=127.0.0.1;U......
  • JDBC连接openGauss6.0和PostgreSQL16.2性能对比
    本文分享自华为云社区《JDBC连接openGauss6.0和PostgreSQL16.2性能对比》,作者:Gauss松鼠会小助手。PostgreSQLvsopenGauss01前置准备安装JDK:详细安装步骤请问度娘,输入能正常返回即已安装[root@db06~]#java-versionopenjdkversion"1.8.0_262"OpenJDKRuntimeEnvir......
  • SSH连接远程仓库
    【1】生成密钥文件在任意位置打开cmd或者gitbashssh-keygen-ted25519-C"[email protected]"在用户目录下的.ssh就会自动生成密钥,打开pub结尾的,复制其内容【2】把公钥配置在gitee账号上【3】删除之前配置的origingitremoteremoveorigin【4】换成ssh地址gitrem......
  • Qt 信号槽连接方式
    Qt的使用这个函数处理信号voidQMetaObject::activate(QObject*sender,intsignalOffset,intlocal_signal_index,void**argv) 多线程情况下:直连或者队列连接使用 queued_activate()处理:阻塞连接(BlockingQueuedConnection)相同线程直接调用,不同线程使用事件处理:......
  • 本地SSH方式连接实例
    通过SSH登录GPUMALL实例介绍通过SSH方式连接到Linux服务器的方法有多种,这里介绍几种常用的SSH远程登录工具,只需要使用其中一种可以登录到GpuMall实例即可。立即免费体验:https://gpumall.com/login?type=register&source=cnblogsWindows系统可以使用:XShell、Mobaxterm、......
  • 开发工具连接实例远程开发
    远程开发主要基于将开发环境(包括代码编辑、编译、运行等)从本地机器转移到远程服务器上,这个过程涉及几个关键组件和概念:立即免费体验:https://gpumall.com/login?type=register&source=cnblogs1.远程服务器远程服务器是托管远程开发环境的中心,可以是一个物理服务器,也可以是云中的......