Graph-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.
int findParent(vector<int>& parent, int k) {
if (parent[k] != k)
parent[k] = findParent(parent, parent[k]);
return parent[k];
} vector<int> findRedundantConnection(vector<vector<int> >& edges) {
vector<int> parent;
for (int i = ; i < ; i++) // 初始化
parent.push_back(i); int point1, point2;
for (int j = ; j < edges.size(); j++) {
point1 = findParent(parent, edges[j][]);
point2 = findParent(parent, edges[j][]);
if (point1 == point2)
return edges[j];
parent[point2] = point1;
}
return vector<int>(, );
}
Graph-684. Redundant Connection的更多相关文章
- 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】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] 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之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
- [LeetCode] Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- [LeetCode] Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
随机推荐
- [NOI.AC]COUNT(数学)
解析: 也可以将所有的可能都计算出来,后进行减法运算. 代码: #include<bits/stdc++.h> using namespace std; #define ll long l ...
- Tomcat连接池配置
今日做了个小网站,数据量不大,但当发布到虚拟主机上之后,接连不断的遇到各种问题. 被折磨了数日后,在网上查了大量的相关资料,现总结如下. 一.项目在上传到远程服务器的过程中,有可能丢失文件,或文件内容 ...
- UUID含义及ubuntu配置系统默认JDK
UUID含义是通用唯一识别码(Universally Unique Identifier) GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随即数来生 ...
- spring-data-jpa+hibernate 各种缓存的配置演示
本文所有测试用代码在https://github.com/wwlleo0730/restjplat 的分支addDB上 目前在使用spring-data-jpa和hibernate4的时候,对于缓存关 ...
- 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)
传送门 主席树经典题目. 直接利用主席树差分的思想判断区间中数的个数是否合法然后决定左走右走就行了. 实际上跟bzoj3524是同一道题. 代码: #include<bits/stdc++.h& ...
- 2018.07.03 POJ 2653 Pick-up sticks(简单计算几何)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Description Stan has n sticks of various leng ...
- freeRadius与NetGear WNAP210的简使用
1.下载安装freeRadius for win 2.2 2.配置client.cnf文件 加入 client 192.168.0.0/16{ secret=1111122222 shortname= ...
- SpringMVC零碎笔记
在web.xml里可以配置webapp的默认首页,格式如下: <welcome-file-list> <welcome-file>index.html</welcome- ...
- Flex box 弹性盒子布局
display: -webkit-flex 标识使用弹性布局 flex: num 占容器的比例 Flex等比划分 导航1 : Flex :1; 导航2 Flex: 2; ————&g ...
- VS 附加不上w3wp.exe
今天调用VS 附加不上w3wp.exe,其他的站点都能附加上,就有一个站附加不上,找了各种可能都没有解决,结果发现是版本被编译成release了,原来的配置都是debug的,不知道被谁给改成relea ...