题目:

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)的更多相关文章

  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 685. Redundant Connection II

    原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...

  4. Java实现 LeetCode 685 冗余连接 II(并查集+有向图)

    685. 冗余连接 II 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着 ...

  5. [LeetCode] 684. Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  6. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  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 85. 冗余连接 II

    题目: 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着N个节点 (节点值 ...

  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 ...

  10. leetcode 684. Redundant Connection

    We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...

随机推荐

  1. 力扣580(MySQL)-统计各专业人数(中等)

    题目: 一所大学有 2 个数据表,分别是 student 和 department ,这两个表保存着每个专业的学生数据和院系数据. 写一个查询语句,查询 department 表中每个专业的学生人数 ...

  2. 力扣535(java)-TinyURL的加密与解密(中等)

    题目: TinyURL 是一种 URL 简化服务, 比如:当你输入一个 URL https://leetcode.com/problems/design-tinyurl 时,它将返回一个简化的URL  ...

  3. 行业 SaaS 微服务稳定性保障实战

    简介: 对于Tob企业而言,稳定性即是生命线.那么,面对商户数目暴增, C 端场景业务不断扩展呢,F6汽车科技又是如何搭建可观测体系呢?一线负责人深度解读实际演进过程! 很多研发人员在日常工作中经常回 ...

  4. 2019-10-31-WPF-设置纯软件渲染

    title author date CreateTime categories WPF 设置纯软件渲染 lindexi 2019-10-31 8:59:2 +0800 2018-04-20 16:36 ...

  5. 2019-8-31-How-to-output-the-target-message-in-dotnet-build-command-line

    title author date CreateTime categories How to output the target message in dotnet build command lin ...

  6. 基于EPCLYPSE的DDS控制器(一)

    软硬件平台 操作系统:windows11 软件平台:vivado2021.1 开发套件 硬件平台:Digilent Eclypse-Z7 Zmod开发套件 vivado 工程搭建 ①直接拉取笔者的三个 ...

  7. 7.prometheus监控--监控docker

    4.监控docker 为了能够获取到Docker容器的运行状态,用户可以通过Docker的stats命令获取到当前主机上运行容器的统计信息,可以查看容器的CPU利用率.内存使用量.网络IO总量以及磁盘 ...

  8. 爽了!免费的SSL,还能自动续期!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 大家好,我是技术UP主小傅哥. 兄弟,当你手里有不少域名,每个域名又配置子域名,那么ssl将是 ...

  9. VSCode 中安装 esp-idf

    一.准备工具 首先需要安装好 VSCode 软件和 esp-idf 环境. 安装 VSCode VSCode 安装比较简单,我就不赘述了,进入官网下载一键安装即可 VSCode官网:https://c ...

  10. CF633H (线段树维护矩乘 + 莫队)

    Fibonacci-ish II 题意:给定一个长度最大为 \(30000\) 的序列,和最多 \(30000\) 个询问,每个询问问某区间 \([L,R]\) 里的数,去掉重复然后排序之后,依次乘上 ...