Files
2020-08-09 00:39:24 +08:00

59 lines
3.8 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [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.
## 题目大意
在本问题中有根树指满足以下条件的有向图该树只有一个根节点所有其他节点都是该根节点的后继每一个节点只有一个父节点除了根节点没有父节点输入一个有向图该图由一个有着 N 个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成附加的边的两个顶点包含在1到N中间这条附加的边不属于树中已存在的边结果图是一个以边组成的二维数组 每一个边的元素是一对 [u, v]用以表示有向图中连接顶点 u and v 和顶点的边其中父节点 u 是子节点 v 的一个父节点返回一条能删除的边使得剩下的图是有 N 个节点的有根树若有多个答案返回最后出现在给定二维数组的答案
注意:
- 二维数组大小的在 3 1000 范围内
- 二维数组中的每个整数在 1 N 之间其中 N 是二维数组的大小
## 解题思路
- 这一题是第 684 题的加强版 684 题中的图是无向图这一题中的图是有向图
- 这一题的解法也是用并查集不过需要灵活一点不要用模板因为在模板中存在路径压缩和 `rank()` 优化这些优化会改变有向边原始的方向所以并查集只需要记录 `parent()` 就够用了
![](https://img.halfrost.com/Leetcode/leetcode_685.png)
- 经过分析可以得到上面这 3 种情况红色的边是我们实际应该删除的先来看情况 2 和情况 3 当不断 `union()` 加入一条边以后会使一个节点的入度变成 2那么记录下这两条边为 `candidate1` `candidate2`将后加入的 `candidate2` 这条边先放在一边继续往下 `union()`如果 `candidate2` 是红色的边那么合并到最后也不会出现任何异常那么 `candidate2` 就是红色的边即找到了要删除的边了如果合并到最后出现了环的问题了那说明 `candidate2` 是黑色的边`candidate1` 才是红色的边那么 `candidate1` 是要删除的边
- 再来看看情况 1如果一路合并到结束也没有发现出现入度为 2 的情况那么说明遇到了情况 1 情况 1 会出现环的情况题目中说如果要删除边就删除最后出现的那条边。**具体实现见代码注释**。