LN : leetcode 684 Redundant Connection
lc 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, 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.
Union Find Accepted
Union Find的关键思想是使各结点依次连结在一起,如果有从1到2的边,则令uni[1] = 2,如果有从2到3的边,则令uni[2] = 3,这样如果此时新加一条从1到3的边,那么从1开始寻找,uni1为2,uni[2]为3,即1已经有了一条从1到3的边,再加会导致形成环路,所以这就是我们要的答案。
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
vector<int> uni(2001, -1);
for (auto edge : edges) {
int head = edge[0], tail = edge[1];
int x = find(uni, head), y = find(uni, tail);
if (x == y) return edge;
uni[x] = y;
}
return {};
}
int find(vector<int>& uni, int num) {
while(uni[num] != -1) {
num = uni[num];
}
return num;
}
};
LN : 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 ...
- 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] 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 ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
随机推荐
- goroutine pool,WaitGroup,chan 示例
服务端高并发编程经常需要写很多goroutine来服务每一个连接,如何正确使用goroutine池是又拍云的工程师们需要考虑的问题,今天这篇文章,分享给同样需要使用go语言的小伙伴们. 文/陶克路 本 ...
- 数据库连接池-配置 wallfilter
使用缺省配置的WallFilter <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSou ...
- JS判断按时间跳转到相应的页面
<!--时间段跳转js--><script language="javaScript" type="text/javascript"> ...
- [Selenium] 如何在老版本的Chrome 浏览器上使用selenium
由于Chrome Driver 只兼容Chrome 浏览器12.0.712.0 和之后的新版本,会因此如果要在老版本的Chrome 浏览器上使用Selenium, 则只能使用 SeleniumRC ...
- BZOJ_1264_[AHOI2006]基因匹配Match_树状数组
BZOJ_1264_[AHOI2006]基因匹配Match_树状数组 Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种 ...
- egret 和cocos2d-x-js哪个目前更稳定更好用? ?
问题: 貌似cocos名气大一些?因为神经猫的大火才知道egret,玩了一下他们的demo,貌似性能一般,不过对flash开发者特别亲切.有人对比过这两个引擎吗?分析下 百牛信息技术bainiu.lt ...
- Java调用Static修饰的本类方法
public class Dy { public static void main(String[] args){ int a=6; int b=5; int result=0; result=Add ...
- (水题)洛谷 - P1603 - 斯诺登的密码
https://www.luogu.org/problemnew/show/P1603 有毒,大小写不检测,句号也不管. #include<bits/stdc++.h> using nam ...
- hdoj5813【构造】
2016 Multi-University Training Contest 7 05 真的真的好菜哇... 思路: 暴力. 我对那些到达目的地少的点做硬性规定就是去比他要到达目的地更少的点,这样一来 ...
- PJzhang:lijiejie的敏感目录爆破工具BBScan
猫宁!!! 参考链接: https://www.freebuf.com/sectool/85729.html https://segmentfault.com/a/1190000014539449 这 ...