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 ...
随机推荐
- 力扣290(java)-单词规律(简单)
题目: 给定一种规律 pattern 和一个字符串 s ,判断 s 是否遵循相同的规律. 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接 ...
- 容器环境自建数据库、中间件一键接入阿里云 Prometheus 监控
------------恢复内容开始------------ 阿里云Prometheus服务4月9日发布重大升级,支持容器环境下一键接入MySQL.Redis.MangoDB.ElasticSearc ...
- SpringCloud 应用在 Kubernetes 上的最佳实践 —— 高可用(弹性伸缩)
作者 | 三未 前言 弹性伸缩是一种为了满足业务需求.保证服务质量.平衡服务成本的重要应用管理策略.弹性伸缩让应用的部署规模能够根据实时的业务量产生动态调整,在业务高峰期扩大部署规模,保证服务不被业务 ...
- mPaas 研发流程和线上运维介绍
简介: mPaas 研发流程和线上运维介绍 一. 背景 金融级移动开发平台 mPaaS[1](Mobile PaaS)为 App 开发.测试.运营及运维提供云到端的一站式解决方案,能有效降低技术门槛. ...
- Spring Boot 微服务性能下降九成!使用 Arthas 定位根因
简介: 接收到公司业务部门的开发反馈,应用在升级公司内部框架后,UAT(预生产)环境接口性能压测不达标. 背景 接收到公司业务部门的开发反馈,应用在升级公司内部框架后,UAT(预生产)环境接口性能压测 ...
- PyFlink 教程(三):PyFlink DataStream API - state & timer
简介: 介绍如何在 Python DataStream API 中使用 state & timer 功能. 一.背景 Flink 1.13 已于近期正式发布,超过 200 名贡献者参与了 Fl ...
- 10个Bug环环相扣,你能解开几个?
简介:由阿里云云效主办的2021年第3届83行代码挑战赛已经收官.超2万人围观,近4000人参赛,85个团队组团来战.大赛采用游戏闯关玩儿法,融合元宇宙科幻和剧本杀元素,让一众开发者玩得不亦乐乎. ...
- Kubernetes 入门教程
简介:本文是一篇 kubernetes(下文用 k8s 代替)的入门文章,将会涉及 k8s 的架构.集群搭建.一个 Redis 的例子,以及如何使用 operator-sdk 开发 operator ...
- 日志服务Dashboard加速
简介: 阿里云日志服务致力于为用户提供统一的可观测性平台,同时支持日志.时序以及Trace数据的查询存储.用户可以基于收集到的各类数据构建统一的监控以及业务大盘,从而及时发现系统异常,感知业务趋势.但 ...
- [FAQ] PHPStorm None project files detection
当你发现在 phpstorm 中编辑项目文件,却提示 None project files detection.. 并且左侧 Project 下面只有文件,没有项目目录了. 此时可以删除项目目录下的 ...