LeetCode 685. Redundant Connection II 冗余连接 II (C++/Java)
题目:
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.
分析:
这道题是LeetCode 684. Redundant Connection 冗余连接(C++/Java)的进阶版,还是利用并查集的思路做,不会的同学可以先看一下前面的题解。
685题将图更改为有向图,最后要求删去一条边得到一颗树,也就是要求只能有一个根节点,每一个节点只有一个父节点。
那么如果在图中有一个节点有两个父结点时,则要删去的边就必定是这两条边中构成环的一条,如果所有的节点都只有一个父结点,那么删去的边就是构成环的那一条边,基于这个思路,先遍历所有的边,存储他们的父亲节点,当发现有节点的父节点已存在时,将原来的父结点和这个节点记做结果1,新的父节点和这个节点记做结果2,在这里我们将后出现的边做一个标记。因为在做并查集时,发现有环的话,无法立刻判断出这两条边到底那条在环中,所以我们将新来的边做标记,在构建并查集时,不将新的边加入,那么如果最后图内无环则直接返回结果2,如果有环且结果1不为空就返回结果1,如果结果1为空则表明不存在多个父结点的节点,那么就和684题一样了,直接返回构成环的边即可。
程序:
C++
class Solution {
public:
vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
int n = edges.size();
vector<int> parents(n+1, 0);
vector<int> root(n+1, 0);
vector<int> res1;
vector<int> res2;
for(auto &edge:edges){
int u = edge[0];
int v = edge[1];
if(parents[v] > 0){
res1 = {parents[v], v};
res2 = edge;
edge[0] = -1;
edge[1] = -1;
}
parents[v] = u;
}
for(auto edge:edges){
int u = edge[0];
int v = edge[1];
if(u < 0 || v < 0)
continue;
if(!root[u]) root[u] = u;
if(!root[v]) root[v] = v;
int pu = find(u, root);
int pv = find(v, root);
//有环
if(pu == pv){
return res1.empty() ? edge : res1;
}
root[pv] = pu;
}
return res2;
}
private:
int find(int node, vector<int>& root){
while(root[node] != node){
root[node] = root[root[node]];
node = root[node];
}
return node;
}
};
Java
class Solution {
public int[] findRedundantDirectedConnection(int[][] edges) {
int n = edges.length;
int[] parents = new int[n+1];
int[] roots = new int[n+1];
int[] res1 = null;
int[] res2 = null;
for(int[] edge:edges){
int u = edge[0];
int v = edge[1];
if(parents[v] > 0){
res1 = new int[]{parents[v], v};
res2 = new int[]{u, v};
edge[0] = -1;
edge[1] = -1;
}
parents[v] = u;
}
for(int[] edge:edges){
int u = edge[0];
int v = edge[1];
if(u < 0 || v < 0)
continue;
if(roots[u] == 0) roots[u] = u;
if(roots[v] == 0) roots[v] = v;
int pu = find(u, roots);
int pv = find(v, roots);
if(pu == pv){
return res1 == null ? edge : res1;
}
roots[pv] = pu;
}
return res2;
}
private int find(int node, int[] roots){
while(node != roots[node]){
roots[node] = roots[roots[node]];
node = roots[node];
}
return node;
}
}
LeetCode 685. Redundant Connection II 冗余连接 II (C++/Java)的更多相关文章
- [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 685. Redundant Connection II
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...
- Java实现 LeetCode 685 冗余连接 II(并查集+有向图)
685. 冗余连接 II 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着 ...
- [LeetCode] 684. 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 ...
- [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 85. 冗余连接 II
题目: 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着N个节点 (节点值 ...
- 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 ...
- leetcode 684. Redundant Connection
We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...
随机推荐
- 力扣1076(MySQL)-员工项目Ⅱ(简单)
题目: 编写一个SQL查询,报告所有雇员最多的项目. 查询结果格式如下所示: 解题思路: 方法一:将两个表联结,以project_id进行分组,统计员工数降序排序,然后筛选出第一条数据. 1 sel ...
- 力扣610(MySQL)-判断三角形(简单)
题目: 表: Triangle 写一个SQL查询,每三个线段报告它们是否可以形成一个三角形. 以 任意顺序 返回结果表. 查询结果格式如下所示. 示例1: 解题思路: 判断是否形成三角形的准则是:两 ...
- 力扣185(MySQL)-部门工资前三高的所有员工(困难)
题目: 表: Employee 表: Department 公司的主管们感兴趣的是公司每个部门中谁赚的钱最多.一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 . 编写一个S ...
- 力扣181(MySQL)- 超过经理收入的员工(简单)
题目: 表:Employee 编写一个SQL查询来查找收入比经理高的员工. 以 任意顺序 返回结果表. 查询结果格式如下所示. 示例 1: 解题思路: 一.[子查询] 先通过子查询找到当前员工的经理 ...
- 力扣784(java)-字母大小写全排列(中等)
题目: 给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串. 返回 所有可能得到的字符串集合 .以 任意顺序 返回输出. 示例 1: 输入:s = " ...
- 力扣233(java)-数字1的个数(困难)
题目: 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例 1: 输入:n = 13输出:6示例 2: 输入:n = 0输出:0 提示: 0 <= n <= ...
- 【ESSD技术解读-03】阿里云块存储企业级特性之异步复制
简介: 在大数据时代,数据就是企业的核心资产,是企业的生命线.在现实世界中,灾难时有发生,当发生灾难时,容灾能力成为企业能否生存的关键.云上容灾服务,通常称为 DRaaS(灾难恢复即服务)不但能够省 ...
- dotnet core 不自动从 https 到 http 的 302 重定向
本文记录一个已知问题,或者准确来说是设计如此的行为,在 dotnet core 下,无论是 dotnet core 3.1 还是 dotnet 5 或 dotnet 6 或 dotnet 7 等,如果 ...
- 使用.NET源生成器(SG)实现一个自动注入的生成器
DI依赖注入对我们后端程序员来说肯定是基础中的基础了,我们经常会使用下面的代码注入相关的service services.AddScoped<Biwen.AutoClassGen.TestCon ...
- golang基础之结构体
匿名结构体 在定义一些临时数据结构等场景下还可以使用匿名结构体. 在函数体内 package main import ( "fmt" ) func main() { //方法一 v ...