LeetCode 685. Redundant Connection II
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/
题目:
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.
The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), with one additional directed 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] that represents a directed edge connecting nodes u and v, where u is a parent of child v.
Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.
Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given directed graph will be like this:
1
/ \
v v
2-->3
Example 2:
Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]
Output: [4,1]
Explanation: The given directed graph will be like this:
5 <- 1 -> 2
^ |
| v
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.
题解:
If it is a invalid tree, there could be 2 cases:
- one node has 2 parents. [i, j], [k, j], two edges both point to j.
- there is cyrcle.
If remove one redundant edge could make it a valid tree.
If case 1 happens, then redundant edge must be either [i, j] or [k, j]. Otherwise, even you remove the redundant edge, [i, j] and [k, j] still point to the same node j and it is still invalid. Thus make them candidate 1 and candidate 2.
We check if cycle exists, if it exists, we check if case 1 happens or not. If no, then edge contecting 2 nodes already within the same union is the redundant edge likeRedundant Connection.
If yes, redundant edge is either candiate 1 or 2. First remove candidate 2 and check if cycle still exists, if no then answer is candidate 2, otherwise it is candidate 1.
Note: only update parent if parent[edge[1]] == 0.
Time Complexity: O(nlogn). find takes O(logn).
Space: O(n).
AC Java:
class Solution {
int [] parent;
public int[] findRedundantDirectedConnection(int[][] edges) {
int n = edges.length;
parent = new int[n+1];
int [] can1 = new int[]{-1, -1};
int [] can2 = new int[]{-1, -1};
for(int i = 0; i<n; i++){
if(parent[edges[i][1]] == 0){
parent[edges[i][1]] = edges[i][0];
}else{
can2 = new int[]{edges[i][0], edges[i][1]};
can1 = new int[]{parent[edges[i][1]], edges[i][1]};
edges[i][1] = 0;
}
}
for(int i = 0; i<=n; i++){
parent[i] = i;
}
for(int [] edge : edges){
if(find(edge[0]) == find(edge[1])){
if(can1[0] == -1){
return edge;
}
return can1;
}
union(edge[0], edge[1]);
}
return can2;
}
private int find(int i){
if(i != parent[i]){
parent[i] = find(parent[i]);
}
return parent[i];
}
private void union(int i, int j){
int p = find(i);
int q = find(j);
parent[q] = p;
}
}
LeetCode 685. Redundant Connection II的更多相关文章
- [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] 684. 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 ...
- LN : leetcode 684 Redundant Connection
lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...
- Java实现 LeetCode 685 冗余连接 II(并查集+有向图)
685. 冗余连接 II 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着 ...
- [Swift]LeetCode685. 冗余连接 II | 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
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 ...
随机推荐
- C++分治策略实现快速排序
问题描述: 给定一个未知顺序的n个元素组成的数组,现要利用快速排序算法对这n个元素进行非递减排序. 细节须知: (1)代码实现了利用递归对数组进行快速排序,其中limit为从已有的随机数文件中输入的所 ...
- SonarQube安装教程与简单使用(基于Centos7,JDK1.8)
SonarQube 若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11686522.html 概念: SonarQube是一种自动 ...
- Golang 常用的第三方包.
Goland 下面这个license server 可用 http://idea.youbbs.org (2018-01-10 04:26:09) http://45.77.127.87:81(201 ...
- httpd服务的配置及应用
一.httpd服务的配置文件 httpd服务的主配置文件通常为httpd根目录下的conf/httpd.conf文件,通过yum安装的httpd服务的主配置路径通常如下: httpd-2.2:/etc ...
- Postman中添加真实请求(Chrome Networks中的全部请求,含https)copy as har
Postman中添加真实请求(Chrome Networks中的全部请求,含https) xyxzfj 关注 2018.05.22 19:44* 字数 559 阅读 1176评论 0喜欢 0 Post ...
- div中的“内容”水平垂直居中
1. div高度自适应的情况 div在不设置高度的时候,会被里面的内容撑开,内容自动填充在div中,无论是一行内容还是多行内容,此时不需要设置垂直居中,内容自动在中间的, 想要看的更直观些,只需要加上 ...
- js模块基础练习题
题目描述 完成函数 createModule,调用之后满足如下要求: 1.返回一个对象 2.对象的 greeting 属性值等于 str1, name 属性值等于 str2 3.对象存在一个 sayI ...
- springmvc与struts2
1. springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过虑器. 2. springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形 ...
- Appscan漏洞 之 加密会话(SSL)Cookie 中缺少 Secure 属性
近期 Appscan扫描出漏洞 加密会话(SSL)Cookie 中缺少 Secure 属性,已做修复,现进行总结如下: 1.1.攻击原理 任何以明文形式发送到服务器的 cookie.会话令牌或用户凭证 ...
- 工厂交接班易出问题?MES系统实现精准对接
工厂交接班制度非常的严格和复杂,而MES系统能让繁琐的交接班流程简单快捷无措.MES系统在发生事件时记录传递事件,还可以主动对事件进行分类和报告.人员可以查看和深入到以前或当前班次的个别事件. 随着工 ...