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
class Solution {
public int[] findRedundantConnection(int[][] edges) {
int[] parent = new int[edges.length + ];
for (int i = ; i < parent.length; i++) parent[i] = i; for (int[] edge: edges){
int f = edge[], t = edge[];
if (find(parent, f) == find(parent, t)) return edge;
else parent[find(parent, f)] = find(parent, t);
} return new int[];
} private int find(int[] parent, int f) {
if (f != parent[f]) {
parent[f] = find(parent, parent[f]);
}
return parent[f];
}
}
Redundant Connection的更多相关文章
- [LeetCode] Redundant Connection 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 ...
- [Swift]LeetCode684. 冗余连接 | 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] 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] 684. 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
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...
- [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 684. Redundant Connection
We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...
随机推荐
- .Net中手动实现AOP
序言 资料 https://www.cnblogs.com/farb/p/AopImplementationTypes.html
- PTA 二叉树路径
二叉树的路径 (25 分) 二叉树是一种普通的数据结构.给出一棵无限的二叉树,节点被标识为一对整数,构造如下: (1)树根被标识为整数对(1,1). (2)如果一个节点被标识为(a,b ...
- Jmeter设置成中文
首次启动Jmeter为中文 选择后即变为中文
- hadoop hdfs hbase优化实例
需求描述: 从hdfs中获取数据,字段url需要计算出url_type 通过进行hive的left outer join ,效率非常低.故将url的类型导入到hbase中,利用hbase快速查询的特点 ...
- LeetCode 48. 旋转图像(Rotate Image)
题目描述 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: ...
- centos7 - mysql修改密码
set password for 'root'@'localhost'=password('MyNewPass4!'); mysql5.7默认安装了密码安全检查插件(validate_password ...
- openerp学习笔记 单据自动编号(编码规则)
说明: 单据自动编码允许定义 单据前缀+按当前年.月.日.时.分.秒+流水号+单据后缀 单据自动编号允许按所有公司统一编号或按分公司单独编号 单据自动编号中的流水号部分未按月重新编号,不断累计,当超出 ...
- 用配置文件里面的参数值替换yaml模板中的变量值【python】
用配置文件里面的参数值替换yaml模板中的变量值[python] #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/9/20 1 ...
- flutter textfield设置高度后内容区无法居中?
textfiled 设置高度后,内容永远无法居中,最后找到原因 decoration: 中有一个 contentPadding属性,设置这个属性对应的Padding值即可
- 美化Eclipse-背景
为了美化Eclipse,请登录主题网站http://www.eclipsecolorthemes.org/ 下载EPF配置文件(截图如下),并导入eclispe即可. 导入方法: (1)从File菜单 ...