leetcode 684. Redundant Connection(多余的连接)

S32K144 ADC硬件触发实战:PDB与TRGMUX配置避坑指南 本文深入解析了S32K144微控制器中ADC硬件触发的两种核心方案:PDB与TRGMUX。通过对比其架构差异、详细配置流程及常见问题排查,提供了从寄存器操作到DMA联动的完整优化指南,旨在帮助开发者解决多通道采样时序不稳、CPU负载过高等实际难题,实现高效稳定的数据采集。 阅读详情

In this problem, a tree is an undirected graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.

Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:

  1
 / \
2 - 3

Note:
The size of the input 2D-array will be between 3 and 1000.
Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

给出一个无向图,找出第一个构成闭环的边

思路:
可用一个邻接链表保存每条边,DFS搜索闭环,O(N2)
这里参考Union-Find解法
union指的是相同root的节点聚为一类,这些节点指向同一root。
find是给一个节点,找到它的parent,进而找到它的root。
如果一条边,它的两个节点指向同一root,比如例子中的2,3,都指向1,那么2,3这条边就构成了闭环,需要去掉。

union的时候如果出现一长串,1指向2,2指向3,…,为了在find中效率更高,在union的时候要把这一连串的节点都直接指向root,这样在find的时候可以一步到位找到root。
当有两个集合,我们要修改size较小的集合中的节点的parent,这样可以节省开销。所以还要保存每个root所连接的所有节点的size(节点数量)。

刚开始的时候每个节点都指向它自己,它自己就是自己的parent,也是root。

root,parent,node的关系是,node的parent可以是root,也可以是其他节点,而最上面那层parent就是root。

    public int[] findRedundantConnection(int[][] edges) {
        int m = edges.length;
        int[] parent = new int[m+1];
        int[] size = new int[m+1];
        
        for(int[] edge : edges) {
            int u = edge[0];
            int v = edge[1];
            if(parent[u] == 0) parent[u] = u;
            if(parent[v] == 0) parent[v] = v;
            
            int pu = findParent(u, parent);
            int pv = findParent(v, parent);
            
            if(pu == pv) return edge;
            
            //较小size的接到较大size的root上
            if(size[pv] > size[pu]) {
                //swap pu and pv
                int tmp = pv;
                pv = pu;
                pu = tmp;
            }
            
            //v 并入u
            parent[pv] = pu;
            size[pu] += size[pv];
        }
        return new int[]{};
    }
    
    int findParent(int node, int[] parent) {
        while(parent[node] != node) {
            parent[node] = parent[parent[node]];
            node = parent[node];
        }
        
        return node;
    }

参考资料

人工智能训练师三级考试大纲深度解读|理论+实操分值分布+六大模块占比 三级人工智能训练师考试分为理论和实操两大模块(各100分,合格线60分)。理论考试包含AI基础(30%)、数据标注(25%)、模型训练(20%)、测试(15%)和系统运维(10%),题型为客观题;实操考核侧重数据标注(40%)、模型测试(35%)和系统运维(25%),采用现场操作形式。复习应优先关注AI基础和数据标注等高分值模块,实操需重点掌握标准流程和结果准确性(占评分70%)。考试要求两模块均达标,理论侧重概念理解,实操强调规范执行与文档完整。 阅读详情

相关推荐

YOLOv5+Astra Pro Plus深度相机:手把手教你实现实时目标检测(附ROS配置技巧)

本文详细介绍了如何将YOLOv5目标检测算法与Astra Pro Plus深度相机结合,构建实时目标检测系统。内容涵盖从硬件选型、环境搭建、ROS配置到YOLOv5部署与深度信息融合的完整流程,并分享了性能优化、避障算法等实战技巧,帮助开发者在机器人视觉等场景中快速实现智能感知应用。

weixin_28292761的博客 406

【LeetCode】每日一题 2024_10_27 冗余连接(并查集)

【LeetCode】每日一题 2024_10_27 冗余连接(并查集)

Locky136的博客 553

基于DFT的scanshift sdc模板(DFT)

摘要:本文详细介绍了DFT测试模式下的SDC约束方法。

murphyrong的博客 909

leetcode算法题--二叉搜索树与双向链表

原文链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/ Node *pre = NULL, *head; Node* treeToDoublyList(Node* root) { if (root == NULL) return NULL; dfs(root); head->left = pre;//head指向头节点,pre指向尾节点 pre-&g

bob的博客 236

Windows10检查Cuda和cuDNN是否安装成功?

简介:根据上篇博客已经完成了CUDA和cuDNN的安装步骤,完成对其的安装。下面将来检查它们是否安装成功。 1 .首先win+R进入CMD,在命令行输入nvcc -V,如下图所示,则表示cuda已经安装成功。 2 .进入到cuda的安装路径,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\demo_suite,找到如下两个.exe文件: 首先执行:deviceQuery.exe,查看是否出现如下界面: 然后执行bandwid

jhsignal的博客 14万+

关于Selenium3在MicrosoftEdge浏览器中出现的问题

前言 最近在做web自动化测试,所用环境是java+selenium3.141.59+MicrosoftEdge(Chromium)浏览器。 在调试的过程中,出现了很多问题。比如selenium3在Edge浏览器中远程调用中,不支持addArguments方法等 问题 1.Edge浏览器弹窗问题 在测试过程中,会出现弹出窗提示 ...

zhucheng1994的博客 2222

LeetCode—684. 冗余连接(Redundant Connection)——分析及代码(Java)

LeetCode—684. 冗余连接[Redundant Connection]——分析及代码[Java]一、题目二、分析及代码1. 并查集(1)思路(2)代码(3)结果三、其他 一、题目 在本问题中, 树指的是一个连通且无环的无向图。 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, …, N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。 结果图是一个以边组成的二维数组。每一个边的元素是一对[u, v] ,满足 u < v,表示连接顶点

江南土豆的博客 498

均值场博弈_平均场博弈(mean field game)

The New Big Fish Called Mean-Field Game Theory原文链接http://www.science4all.org/article/mean-field-games/​www.science4all.org主要内容在经典博弈论中,它对附近其他鱼类的行为做出反应。这是非常复杂的,因为不同的鱼类之间会有大量的相互作用。这意味着经典博弈论对应于一长串高度耦合的方程。...

weixin_34773479的博客 3695

算法--路径总和

描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。 说明: 叶子节点是指没有子节点的节点。 示例 分析 从根节点开始,使用递归,判断左节点或者右节点到叶子节点是否存在sum-root.val即可。 实现 class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) { return fa

CodersCoder的博客 856

【Python深度学习】做深度学习训练时,如果数据集太大,无法一次性加载进GPU,应该如何做?

方法核心思想优点缺点适用场景实现复杂度小批量训练(Mini-Batch Training)与数据加载器(DataLoader)将数据集划分为小批量,每次只加载一个小批量到GPU中进行训练。- 易于实现- 是深度学习的标准做法- 支持多进程加载- 内存可能成为瓶颈- 需要调整批量大小避免内存不足适用于大部分场景低数据分片与分布式训练(Data Sharding & Distributed Training)将数据集分片,每个GPU或节点处理其中一部分数据,分布式训练。

若北辰 1158

《RT-DETR魔术师》专栏介绍 & CSDN独家改进创新实战 & 专栏目录

通过本专栏的阅读,后续你也可以自己魔改网络,在网络不同位置(Backbone、head、neck、loss等)进行魔改,实现创新!!!

①订阅者答疑群聊服务;②FDE:工业缺陷检测项目;③paper/创新算法指导;④免费提供YOLO创新涨点推荐顾问Skill和AI小怪兽数字分身; 7423

Redundant Connection

In this problem, a tree is anundirectedgraph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additi...

leetcode 解题思路 255

【ROS2实体机械臂驱动】封装珞石ROS2驱动库ros2_rokae_driver_lib

这一篇文章将封装为,和之前一篇文章《ROS2 编译、打包、封装三方库(Knet为例)_ros插件编译成静态库-CSDN博客》写的封装,区别在于优化了cmake dependencies和include等属性和宏定义,同时对于多库内部依赖的情况做了特定处理,更加方便外部调用。其中珞石的编译参考博主之前的文章rokae xCoreSDK C++测试使用,这里不再做赘述。通过这篇文章对于如何打包机械臂厂家提供的三方库做了进一步说明,方便用于不同机械臂厂家的三方库封装调用。!!

机器人梦想家 Bing Lee 1416

LeetCode //C - 684. Redundant Connection

【代码】LeetCode //C - 684. Redundant Connection。

Made in Code 1357

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Leetcode算法学习日志-684 Redundant Connection

Leetcode 684 Redundant Connection 题目原文 In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with dis

Zarlove的博客 672

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 684. Redundant Connection 解题报告(python)

684. Redundant Connection Redundant Connection python solution 题目描述 n this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tr...

Orientliu96的博客 430

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Leetcode 684. Redundant Connection

684. Redundant Connection 题目 In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1,...

huang_j_j的博客 289

LeetCode - 684. Redundant Connection (DFS | 并查集)

LeetCode - 684. Redundant Connection (DFS | 并查集) DFS 并查集 题目链接 题目 DFS 思路: 每次添加一条边,然后判断加上这条边之后会不会构成环; 判断一个图有没有环用dfs,这里需要维护一个pre变量,表示上次访问的节点,然后使用vis数组标记以及访问的节点,如果再次访问到,就表明有环了; class Solution { ...

zxzxin的博客~ 905

[leetcode]684. Redundant Connection(Python)

[leetcode]684. Redundant Connection题目描述题目理解解题思路并查集Time 题目描述 Category:Medium Tree In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that st...

La_Ragazza的博客 544

Leetcode 684. Redundant Connection (python)

Leetcode 684. Redundant Connection题目解法1:dfs解法2:unionfind 题目 解法1:dfs 一次构建图,返回第一组成环的边。 class Solution: def findRedundantConnection(self, edges: List[List[int]]) -> List[int]: def dfs(source,target): if source in seen:

qq_37821701的博客 736

LeetCode 684. Redundant Connection解题思路

LeetCode 684. Redundant Connection解题思路1.题目要求2.分析:3.结果 1.题目要求 link:LeetCode684 In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that starte...

xiaominmmmmmm的博客 408

LeetCode 684.Redundant Connection【Java】

题目描述 684. 冗余连接 AC代码 /* 解题思路:并查集 */ class Solution { int[] pre; public int find(int x){ if(pre[x]!=x) pre[x]=find(pre[x]); return pre[x]; } public int[] f...

NayelyA的博客 220

【leetcode】【mudium】684. Redundant Connection​​​​​​​

684. Redundant Connection In this problem, a tree is anundirectedgraph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values...

lemonade13的博客 150

【Leetcode】684. Redundant Connection

题目地址: https://leetcode.com/problems/redundant-connection/ 给定一个无向图,以二维数组,每个位置存一条边的形式给出。问删去哪条边,这个图会变成一棵树。题目保证答案存在。并且返回那个下标最大的满足条件的边。 法1:DFS。题目的本质是找圈,所以可以用DFS来做。先从一个顶点开始遍历,找到圈后返回路径,然后解析出圈,接着再找到下标最大的边即可。代...

数学、算法爱好者的博客 349

LeetCode-684. Redundant Connection [C++][Java]

You are given a graph that started as a tree withnnodes labeled from1ton, with one additional edge added. The added edge has twodifferentvertices chosen from1ton, and was not an edge that already existed. The graph is represented as an arrayedge

贫道绝缘子的博客 376

Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图。 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。 结果图是一个以边组成的二维数组。每一个边的元素是一对[u,...

denghan1065的博客 390
上一篇: leetcode 671. Second Minimum Node In a Binary Tree(二叉树中第二大的值)
下一篇: leetcode 685. Redundant Connection II(多余的连接之二)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址