题目:

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

Example 2:

Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
| |
4 - 3

分析:

给定一个边的序列来构建一个无向图,求出序列中第一条使得无向图成为有环无向图的边。

可以利用dfs来进行搜索,每新加入一条边,查看原来的无向图中是否存在这条边两个顶点间连通分量,如果有的话就会构成环。

dfs的时间复杂度为O(n^2),我们可以利用并查集的思想解决这道题。

以[1,2], [1,3], [2,3]为例。

我们新开辟一个数组用来保存节点间的关系,新加入边[1,2]时。

如果parents数组为0,则将对应索引的值初始化为自身,也就代表两个结点指向的是自己,利用并查集的思想,连通的节点之间所属同一集合。对两个结点进行查询,返回他们最终的父结点,如果两个结点最终的父结点相同,代表他们在同一个集合中,无向图加入这条边就有环了,如果没有的话,将这两个集合合并。

然后加入边[1,3],结果如下图

查找1和3的父结点,分别返回2,和3,他们不相同,将两个集合合并,也就是将2的父亲标记为3

最后加入边[2,3],我们通过上面的图可以发现2,3的父结点最终都是3,是相同的,证明他们在同一集合中,直接返回这条边即可。

在这里并查集可以进行一定的优化,例如合并时,可以将容量小的集合合并到大的集合中,这样修改结点关系的操作较小,而且每次搜索最终父结点时,可以在查询父结点时,同时修改其父结点的关系,减少下次查询消耗的时间。

程序:

C++

class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
vector<int> parents(edges.size()+, );
for(auto edge:edges){
int u = edge[];
int v = edge[];
if(!parents[u])
parents[u] = u;
if(!parents[v])
parents[v] = v;
int pu = find(u, parents);
int pv = find(v, parents);
if(pu == pv)
return edge;
parents[pu] = pv;
}
return {};
}
private:
int find(int node, vector<int> &parents){
while(node != parents[node]){
parents[node] = parents[parents[node]];
node = parents[node];
}
return node;
}
};

Java

class Solution {
public int[] findRedundantConnection(int[][] edges) {
int[] parents = new int[edges.length+1];
for(int[] edge:edges){
int u = edge[0];
int v = edge[1];
if(parents[u] == 0)
parents[u] = u;
if(parents[v] == 0)
parents[v] = v;
int pu = find(u, parents);
int pv = find(v, parents);
if(pu == pv)
return edge;
parents[pu] = pv;
}
return null;
}
private int find(int node, int[] parents){
while(node != parents[node]){
parents[node] = parents[parents[node]];
node = parents[node];
}
return node;
}
}

LeetCode 684. Redundant Connection 冗余连接(C++/Java)的更多相关文章

  1. [LeetCode] 684. Redundant Connection 冗余的连接

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

  2. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  3. leetcode 684. Redundant Connection

    We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...

  4. [LeetCode] 685. Redundant Connection II 冗余的连接之 II

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  5. [LeetCode] Redundant Connection 冗余的连接

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

  6. [LeetCode] 685. Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  7. LeetCode 685. Redundant Connection II

    原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...

  8. 【LeetCode】684. Redundant Connection 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  9. 684. Redundant Connection

    https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...

随机推荐

  1. [AI开发]小型数据集解决实际工程问题——交通拥堵、交通事故实时告警

    这篇文章其实主要是想介绍在深度学习过程中如何使用小型数据集,这种数据集样本数量一般在1000以下,有时候甚至只有几百.一般提到神经网络,大家都会说数据量越丰富,准确性越高,但是实际工作中,可能收集不了 ...

  2. 字符串转hash

    #include<bits/stdc++.h> using namespace std; unsigned hash[]; ; int ans; int main() { ;k<=; ...

  3. 【Tool】---SVN的超级简单并具体得使用介绍

    又一次被打脸,笔者表示再也不相信自己的记性了.简单的SVN隔了一段时间后,由于项目的需要要重新简历代码库,竟然一下子又忘了.天那,这就好比战士上了战场发现没带枪,这能行吗?因此,趁着今天又简短的复习了 ...

  4. [洛谷P4178] Tree (点分治模板)

    题目略了吧,就是一棵树上有多少个点对之间的距离 \(\leq k\) \(n \leq 40000\) 算法 首先有一个 \(O(n^2)\) 的做法,枚举每一个点为起点,\(dfs\) 一遍可知其它 ...

  5. Ubuntu中部署Django项目的配置与链接MySQL

    Django的简介 MVT模式的介绍创建项目的虚拟环境 本次使用的是pip安装 一.更新 sudo apt update 二.安装pip sudo apt install python3-pip 三. ...

  6. MVC 记录

    ASP.NET MVC框架提供了一个帮助我们构造Html元素的类:TagBuilder ps url cnblogs.com/yibinboy/articles/5187682.html HttpRu ...

  7. cmd命令行窗口和文件目录资源管理器快速切换

    本文主要描述如何在指定目录下快速打开当前路径的命令行窗口和在命令行中快速打开指定目录的资源管理器两种快捷方法. 1.在指定目录下快速打开当前路径的命令行窗口 2.在命令行中快速打开当前目录的资源管理器 ...

  8. selenium,xpath路径中引入变量

    比如,我需要获取每一条微博的阅读数,总不可能所有微博都找出xpath,然后获取阅读数 找规律 “//*[@id='Pl_Official_MyProfileFeed__20']/div/div[2]/ ...

  9. Solution: 题解 CF1196E Connected Component on a Chessboard

    感觉这题还可以 因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题 从贪心的角度考虑,如果要使相差数量巨大的\(b\)和\(w\)能够成功放下来,应该使这些方块尽量分散(似乎有点 ...

  10. Http请求特殊符号变空格

    Http请求特殊符号变空格 今天在调试客户端向服务器传递参数时,url中的参数值出现+,空格,/,?,%,#,&等特殊符号的时候就自动变成空格,在服务器端无法获得正确的参数值.解决方法如下: ...