原题链接在这里: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;
}
}

类似Redundant Connection.

LeetCode 685. Redundant Connection II的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode] 684. Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  4. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  5. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  6. Java实现 LeetCode 685 冗余连接 II(并查集+有向图)

    685. 冗余连接 II 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着 ...

  7. [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 ...

  8. leetcode 684. Redundant Connection

    We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...

  9. 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 ...

随机推荐

  1. Label&Button

    Button中的bg参数设置按钮背景颜色,fg参数设置字体颜色 pack中的fill参数告诉Packer让QUIT按钮占据剩余的水平空间,而expand参数则引导它填充整个水平可视空间,将按钮拉伸到左 ...

  2. me.chanjar.weixin.common.error.WxErrorException: {"errcode":40013,"errmsg":"invalid appid hint: [xxxxxxxxxx]"}

    错误解决思路: 1.看看appid和appsecret的配置信息是否正确 2.查看前后端通信的http或者https协议是否正确( http://xxxxxxx 写成https://xxxxxxx)

  3. 2. 运行Spark Streaming

    2.1 IDEA编写程序 Pom.xml加入以下依赖: <dependency> <groupId>org.apache.spark</groupId> <a ...

  4. JavaScript 的 深拷贝和浅拷贝

    深拷贝和浅拷贝都是针对的引用类型, JS中的变量类型分为值类型(基本类型)和引用类型: 对值类型进行复制操作会对值进行一份拷贝,而对引用类型赋值,则会对地址进行拷贝,最终两个变量指向同一份数据 一.先 ...

  5. Drools入门

    文章转载自:http://cwqcwq.iteye.com/blog/397869 一.背景知识:  1.什么是规则引擎  Java规则引擎起源于基于规则的专家系统,而基于规则的专家系统又是专家系统的 ...

  6. 移动端开发之响应式开发和bootstrap基础

    响应式开发 (就是利用媒体查询针对不同宽度的设备进行布局和样式的设置,从而设配不同设备的目的) 响应式布局容器响应式需要一个父级作为布局容器,来配合子级元素来实现变化效果 原理:不同屏幕下,通过媒体查 ...

  7. Jmeter学习笔记(二十)——后置处理器XPath Extractor使用

    一.背景 在使用过程某些操作步骤与其相邻步骤存在一定的依赖关系,需要需要将上一个请求的响应结果作为下一个请求的参数. Jmeter中后置处理器正则表达式提取器和XPath Extractor都可以将页 ...

  8. java系统化基础-day01-基础语法知识

    1.学前必看 该课程将系统化的讲解java基础,但是该课程并不适合零基础的学员,因为在整个java学习体系中我们是按照实际生产设计, 主体思路是以完成某个业务为主线,用到什么技术就学什么技术,即带着问 ...

  9. [LeetCode] 19. 删除链表的倒数第N个节点 ☆☆☆

    描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表 ...

  10. SPI bus 的收发编程

    https://linux-sunxi.org/SPIdev The SPI bus (or Serial Peripheral Interface bus) is a synchronous ser ...