首页 > 其他分享 >[LeetCode] 684. Redundant Connection

[LeetCode] 684. Redundant Connection

时间:2023-01-22 05:11:21浏览次数:53  
标签:node parent int graph 节点 Connection edges 684 LeetCode

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

You are given a graph that started as a tree with n nodes labeled from 1 to 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 graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

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 input.

Example 1:

Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]

Example 2:

Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]

Constraints:

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ai < bi <= edges.length
  • ai != bi
  • There are no repeated edges.
  • The given graph is connected.

冗余连接。

树可以看成是一个连通且 无环 的 无向 图。

给定往一棵 n 个节点 (节点值 1~n) 的树中添加一条边后的图。添加的边的两个顶点包含在 1 到 n 中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 n 的二维数组 edges ,edges[i] = [ai, bi] 表示图中在 ai 和 bi 之间存在一条边。

请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/redundant-connection
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是并查集。这是一道典型的并查集的题目。题目说给了一棵树但是多一条边,注意如果是树的话,如果节点数为 n 的话,那么边的数量是 n - 1。

我们用并查集的模板代码去遍历每条边,每条边都有两个端点 node,我们用并查集的方法去找每个 node 的父节点并记录下来。当我们遍历到某条边,发现这条边上的两个 node 的父节点是一样的时候,就说明这条边是多余的。

时间O(nlogn) - n个点,每个点找自己的父节点的复杂度 ≈ logn

空间O(n)

Java实现

 1 class Solution {
 2     public int[] findRedundantConnection(int[][] edges) {
 3         int[] parent = new int[2001];
 4         for (int i = 0; i < parent.length; i++) {
 5             parent[i] = i;
 6         }
 7         
 8         for (int[] e : edges) {
 9             int from = e[0];
10             int to = e[1];
11             if (find(from, parent) == find(to, parent)) {
12                 return e;
13             }
14             union(from, to, parent);
15         }
16         return new int[2];
17     }
18     
19     private int find(int node, int[] parent) {
20         while (node != parent[node]) {
21             node = parent[node];
22         }
23         return node;
24     }
25     
26     private void union(int a, int b, int[] parent) {
27         int aRoot = find(a, parent);
28         int bRoot = find(b, parent);
29         if (aRoot == bRoot) {
30             return;
31         }
32         parent[aRoot] = bRoot;
33     }
34 }

 

LeetCode 题目总结

标签:node,parent,int,graph,节点,Connection,edges,684,LeetCode
From: https://www.cnblogs.com/cnoodle/p/17064185.html

相关文章

  • leetcode笔记——328周赛
    1.二维前缀和,二维差分304.二维区域和检索-矩阵不可变-力扣(LeetCode)二维前缀和怎么处理,注意加减的下标 2.2537.统计好子数组的数目-力扣(LeetCode)首先看到子数......
  • 【双指针】LeetCode 409. 最长回文串
    题目链接409.最长回文串思路遍历字符串过程中统计字符出现个数,如果达到2则说明可以放到回文串的两端,需要result+=2。遍历完之后的回文串如果长度小于s,说明s中存......
  • LeetCode.541 反转字符串II
    1.题目给定一个字符串s和一个整数k,从字符串开头算起,每计数至2k个字符,就反转这2k字符中的前k个字符。如果剩余字符少于k个,则将剩余字符全部反转。如果剩余字符小......
  • LeetCode.面试题02.05-链表求和-题解分析
    题目来源面试题02.05.链表求和题目详情给定两个用链表表示的整数,每个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并......
  • [LeetCode] 1824. Minimum Sideway Jumps
    Thereisa 3laneroad oflength n thatconsistsof n+1 points labeledfrom 0 to n.Afrog starts atpoint 0 inthe second lane andwantsto......
  • LeetCode.383 赎金信
    1.题目给你两个字符串:ransomNote和magazine,判断ransomNote能不能由magazine里面的字符构成。如果可以,返回true;否则返回false。magazine中的每个字符只能在rans......
  • 【LeetCode链表#12】链表相交
    链表相交同:160.链表相交力扣题目链接(opensnewwindow)给你两个单链表的头节点headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回n......
  • LeetCode——刷题笔记二
         ......
  • LeetCode.202 快乐数
    1.题目编写一个算法来判断一个数n是不是快乐数。「快乐数」 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为1,也......
  • 【DFS】LeetCode 951. 翻转等价二叉树
    题目链接951.翻转等价二叉树思路如果二叉树root1,root2根节点值相等,那么只需要检查他们的孩子是不是相等就可以了。如果root1或者root2是null,那么只有在他们都......