原题链接在这里: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. 手撕面试官系列(九):分布式限流面试专题 Nginx+zookeeper

    Nginx专题 (面试题+答案领取方式见侧边栏) 1.请解释一下什么是 Nginx?2.请列举 Nginx 的一些特性.3.请列举 Nginx 和 Apache 之间的不同点4.请解释 Nginx 如 ...

  2. GridControl单元格编辑验证的方法

    本文实例演示了DevExpress实现GridControl单元格编辑验证的方法,比较实用的功能,具体方法如下: 主要功能代码如下: /// <summary> /// 自定义单元格验证 ...

  3. 【题解】Luogu P5339 [TJOI2019]唱、跳、rap和篮球

    原题传送门 这题zsy写的是\(O(n^2)\),还有NTT\(O(n^2\log n)\)的做法.我的是暴力,\(O(\frac{a b n}{4})\),足够通过 考虑设\(f(i)\)表示序列中 ...

  4. ELK 索引生命周期管理

    kibana 索引配置 管理索引 点击设置 --- Elasticsearch 的 Index management 可以查看 elk 生成的所有索引 (设置,Elasticsearch ,管理) 配 ...

  5. MOOC C++笔记(六):多态

    多态 虚函数 在类的定义中,前面有virtual关键字的成员函数就是虚函数. virtual关键字只用在类定义里的函数声明中,写函数体时不用. 构造函数和静态成员函数不能是虚函数. 多态的表现形式 基 ...

  6. MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  7. 分享AWS网站

    1.AWS服务运行状况检测网站:   https://status.amazonaws.cn/ 2.AWS架构白皮书:https://aws.amazon.com/cn/architecture/?a ...

  8. 解读HTTP/3

    技术的发展总是让人目不暇接,2018年10月,HTTP/3又发布了.虽然已经有一些中文技术媒体做了报道,但大多数是翻译的,而且内容大同小异.最近我专门学习了点关于HTTP/3的知识,在这里随便写写,和 ...

  9. 【RAC】 RAC For W2K8R2 安装--卸载(八)

    [RAC] RAC For W2K8R2 安装--卸载(八) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它 ...

  10. 【OGG】RAC环境下配置OGG单向同步 (四)

    [OGG]RAC环境下配置OGG单向同步 (四) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的 ...