LeetCode 684. Redundant Connection 冗余连接(C++/Java)
题目:
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)的更多相关文章
- [LeetCode] 684. Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- LN : leetcode 684 Redundant Connection
lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...
- leetcode 684. Redundant Connection
We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...
- [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 ...
- [LeetCode] Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- [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 ...
- LeetCode 685. Redundant Connection II
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...
- 【LeetCode】684. Redundant Connection 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 684. Redundant Connection
https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...
随机推荐
- 【转】JavaScript 3D图表
文章系本人原创,转载请保持完整性并注明出自<四火的唠叨> 在说3D图表以前,首先要明确两个概念,一个是数据的维度,一个是呈现数据载体的维度.对于数据的维度,一维的数据呈现,但是呈现的载体是 ...
- 如何使用poi在word表格中插入行的4种方法
本文记录了,在word表格中插入新行的几种方法.直接上代码说明 table.addNewRowBetween 没实现,官网文档也说明,只有函数名,但没具体实现,但很多文章还介绍如何使用这个函数,真是害 ...
- Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档
前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoot 集成 Swagger2 的教程. 什么是 Swagger2 Swagger ...
- React躬行记(16)——React源码分析
React可大致分为三部分:Core.Reconciler和Renderer,在阅读源码之前,首先需要搭建测试环境,为了方便起见,本文直接采用了网友搭建好的环境,React版本是16.8.6,与最新版 ...
- 从Main读取appsetting
using System; using System.Configuration; using Newtonsoft.Json.Linq; using System.Net.Http; using S ...
- 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第一节:整体思路
在构建本章节内容的时候,笔者也在想一个问题,究竟什么样的采集器框架,才能算得上是一个“全能”的呢?就我自己以往项目经历而言,可以归纳以下几个大的分类: 根据通讯协议:HTTP的.HTTPS的.TCP的 ...
- 三个实用的javascript小技巧
从后向前获取数组元素 如果你想从后向前获取一个数组的元素,可以这样写: var newArray = [1, 2, 3, 4] console.log(newArray.slice(-1)) // [ ...
- python sys.modules 和 sys.path 及 __name__
1.sys.modules 存放已经缓存的模块 值是dict 2.sys.path 搜索路径 值是list 3.if __name__= __main__ 可以看成python的程序入口,如果直接执行 ...
- Java入门 - 高级教程 - 07.多线程
原文地址:http://www.work100.net/training/java-multi-threading.html 更多教程:光束云 - 免费课程 多线程 序号 文内章节 视频 1 概述 2 ...
- selenium之窗口滚动
在这里和大家分享一下,selenium里面常用于处理窗口滚动的方法. location_once_scrolled_into_view 一般用于定位窗口底部元素.将窗口拉到最底部. window.sc ...