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 ...
随机推荐
- 在kubernetes集群中使用虚拟节点创建1万Pod-支持在线教育业务
使用虚拟节点提升k8s集群容量和弹性 在kubernetes集群中添加虚拟节点的方式已被非常多的客户普遍使用,基于虚拟节点可以极大提升集群的Pod容量和弹性,灵活动态的按需创建ECI Pod,免去集群 ...
- OceanBase首次阐述战略:继续坚持自研开放之路 开源300万行核心代码
简介: 在数据库OceanBase3.0峰会上,蚂蚁集团自主研发的分布式数据库OceanBase首次从技术.商业和生态三个维度对未来发展战略进行了系统性阐述.同时,OceanBase宣布正式开源,并成 ...
- [GPT] 网页中某些dom内容是通过 js 数据异步渲染的,nodejs 怎么获取网页解析这些数据
要处理使用JavaScript异步渲染内容的网页,您可以在 JavaScript 蜘蛛中使用 Puppeter 或 Playwright 等无头浏览器来获取网页,然后与动态渲染的内容进行交互. 下 ...
- Java面试题:SimpleDateFormat是线程安全的吗?使用时应该注意什么?
在日常开发中,我们经常会用到时间,我们有很多办法在Java代码中获取时间.但是不同的方法获取到的时间的格式都不尽相同,这时候就需要一种格式化工具,把时间显示成我们需要的格式. 最常用的方法就是使用Si ...
- Unity 热更--AssetBundle学习笔记 1.0【AB包资源加载工具类的实现】
工具类封装 通过上文中对AB包加载API的了解和简单使用,对AB包资源加载的几种方法进行封装,将其写入单例类中,如代码展示. 确保每个AB资源包只加载一次: 在LoadAssetBundleManag ...
- 【动画进阶】巧用 CSS/SVG 实现复杂线条光效动画
最近,群里在讨论一个很有意思的线条动画效果,效果大致如下: 简单而言,就是线条沿着不规则路径的行进动画,其中的线条动画可以理解为是特殊的光效. 本文,我们将一起探索,看看在不使用 JavaScript ...
- installshield 64位系统操作注册表遇到的问题
最近在研究IS脚本设置jdk环境变量问题,在使用RegDBKeyExist判断注册表中项的时候一直找不到,翻找文档后发现64位的操作系统需要设置 REGDB_OPTIONS. "SOFTWA ...
- Java面试题:如果你这样做,你会后悔的,两次启动同一个线程~~~
当一个线程被启动后,如果再次调start()方法,将会抛出IllegalThreadStateException异常. 这是因为Java线程的生命周期只有一次.调用start()方法会导致系统在新线程 ...
- 2022年windows的Visual Studio常用插件及使用手册
前景提要 Viusual Studio 是一款很好用的C/C++集成开发工具,具有强大的扩展功能,好用的插件,但是,很多人都是只写了有什么插件,但是,没写怎么使用这种插件,使得使用的时候很是不方便,所 ...
- pageoffice6 版本实现word 文件添加水印
在很多场景下,Word文档正式发文之前,或者说形成最终文档之前,常常需要往Word文件中添加水印,并且会根据文件类型或内容的不同,需要添加的水印也不一样. 添加水印是Word软件里的一个简单功能,直接 ...