【LeetCode】684. Redundant Connection 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/redundant-connection/description/
题目描述
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
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.
题目大意
给出了一个有环无向图的各个边,找出能去除的边,使得这个图不含环(即为树)。
注意,这个图中的各个节点是1~N,总共有N条边,即只多了一条边。
解题方法
并查集
上一次看这个题的时候,我知道使用并查集
去做,但是并没有做出来。这次再次心平气和的看的时候,已经能一遍写出来了。
关于并查集,这个知识点有点大。简而言之,告诉你一条边,去集合里查找这条边的两个节点分别属于哪个树。根据是否属于同一个树,做后续的判断。我之前的一篇文章讲述了并查集的一种应用:【九度OJ】题目1012:畅通工程 解题报告。更多的资料,可以看《计算机考研——机试指南》。
下面的代码实现了并查集查找根节点的代码,并且做了路径压缩,防止树太高导致查找根节点缓慢。
具体到这个题,虽然说是返回最后一个边,但我们知道只需要去除一条边就够了,之前的边不会构成环,直至多余的那条边出现。
另外要注意,当一条边的左右节点的根节点不同时,要把他们设置相同,这样等下次判断某条边的左右节点相同的情况时,说明是多余的那条边了。
python代码如下:
class Solution:
def findRedundantConnection(self, edges):
"""
:type edges: List[List[int]]
:rtype: List[int]
"""
tree = [-1] * (len(edges) + 1)
for edge in edges:
a = self.findRoot(edge[0], tree)
b = self.findRoot(edge[1], tree)
if a != b:
tree[a] = b
else:
return edge
def findRoot(self, x, tree):
if tree[x] == -1: return x
else:
root = self.findRoot(tree[x], tree)
tree[x] = root
return root
C++代码如下:
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
const int N = 1001;
m_ = vector<int>(N, 0);
for (int i = 0; i < N; ++i) {
m_[i] = i;
}
for (auto edge : edges) {
if (!u(edge[0], edge[1]))
return edge;
}
return {0, 0};
}
private:
vector<int> m_;
int f(int a) {
if (m_[a] != a)
m_[a] = f(m_[a]);
return m_[a];
}
bool u(int a, int b) {
int fa = f(a);
int fb = f(b);
if (fa == fb)
return false;
m_[fa] = b;
return true;
}
};
日期
2018 年 5 月 28 日 —— 太阳真的像日光灯~
2019 年 1 月 25 日 —— 这学期最后一个工作日
【LeetCode】684. Redundant Connection 解题报告(Python & C++)的更多相关文章
- 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 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- 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 684. Redundant Connection 冗余连接(C++/Java)
题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given in ...
- [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】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- Oracle-连接多个字段
0.函数concat(A,B)作用:链接字符串 区别: Oracle中:CONCAT()只允许两个参数:(貌似可以内嵌) Mysql中:CONCAT()可以连接多个参数: 1.用||符号进行拼接 例子 ...
- 我可以减肥失败,但我的 Docker 镜像一定要瘦身成功!
作者|徐伟 来源|尔达 Erda 公众号 简介 容器镜像类似于虚拟机镜像,封装了程序的运行环境,保证了运行环境的一致性,使得我们可以一次创建任意场景部署运行.镜像构建的方式有两种,一种是通过 do ...
- Factorization
Factorization or factoring consists of writing a number or another mathematical object as a product ...
- day08 Nginx模块
day08 Nginx模块 lnmp架构 l :Linux n :Nginx m :MySQL p :Python/PHP lnmp架构:是最简单的架构 Nginx中的模块(Python模块):前提是 ...
- R语言学习记录(二)
4.对象改值 4.1.就地改值 比如: vec <- c(0,0,0,0,0,0,0) vec[1]<-100 #vec向量的第一个值就变为100 ####对于数据框的改值的方法,如下面的 ...
- 零基础学习java------20---------反射
1. 反射和动态代理 参考博文:https://blog.csdn.net/sinat_38259539/article/details/71799078 1.0 什么是Class: 我们都知道,对象 ...
- 【STM8】外挂存储器W25Q16
好像有几张图片被强制缩小了?看到这篇博客的人先对你们说声抱歉,我不知道怎么设置 文字就可以很长(文章宽度的全部),图片就只有文章宽度的2/3宽度 开新分页应该就是原始尺寸了,这点还是和大家说抱歉... ...
- Spring整合Ibatis之SqlMapClientDaoSupport
前言 HibernateDaoSupport SqlMapClientDaoSupport . 其实就作用而言两者是一样的,都是为提供DAO支持,为访问数据库提供支持. 只不过HibernateD ...
- python数据预处理和特性选择后列的映射
我们在用python进行机器学习建模时,首先需要对数据进行预处理然后进行特征工程,在这些过程中,数据的格式可能会发生变化,前几天我遇到过的问题就是: 对数据进行标准化.归一化.方差过滤的时候数据都从D ...
- Moment.js使用笔记
零.前情提要 上个月开发了数据平台,用的框架是vue + Ant Design of Vue,其中用了组件[range-picker]日期选择框,涉及到时间方法就去看了momentJS,以此记录~ 如 ...