LeetCode 2359. 找到离给定两个节点最近的节点 基环树

2023-11-09 21:20

本文主要是介绍LeetCode 2359. 找到离给定两个节点最近的节点 基环树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基环树

在这里插入图片描述

  1. 对于有向图来说:基环树就是一个环上挂了一堆树,每个节点只有一个出边,可能有环
  2. 对于无向图来说:n个点n条边的联通,一定是一个基环树
    在这里插入图片描述

题目描述

给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,每个节点 至多 有一条出边。

有向图用大小为 n 下标从 0 开始的数组 edges 表示,表示节点 i 有一条有向边指向 edges[i] 。如果节点 i 没有出边,那么 edges[i] == -1 。

同时给你两个节点 node1 和 node2 。

请你返回一个从 node1 和 node2 都能到达节点的编号,使节点 node1 和节点 node2 到这个节点的距离 较大值最小化。如果有多个答案,请返回 最小 的节点编号。如果答案不存在,返回 -1 。

注意 edges 可能包含环。

示例 1:

输入:edges = [2,2,3,-1], node1 = 0, node2 = 1
输出:2
解释:从节点 0 到节点 2 的距离为 1 ,从节点 1 到节点 2 的距离为 1 。
两个距离的较大值为 1 。我们无法得到一个比 1 更小的较大值,所以我们返回节点 2 。
示例 2:

输入:edges = [1,2,-1], node1 = 0, node2 = 2
输出:2
解释:节点 0 到节点 2 的距离为 2 ,节点 2 到它自己的距离为 0 。
两个距离的较大值为 2 。我们无法得到一个比 2 更小的较大值,所以我们返回节点 2 。

提示:

n == edges.length
2 <= n <= 105
-1 <= edges[i] < n
edges[i] != i
0 <= node1, node2 < n

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-closest-node-to-given-two-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。在这里插入图片描述
数据量10的五次方,要控制在nlogn以内。
思路很简单,就是求出来每个点到两个起点的距离,然后再遍历每个点,在维护距离最大值的同时,维护一个id的最小值。

求每个点到起点的最短距离,由于每个边权重一样,直接BFS就可以,但是由于是基环树,起点到每个点的路径是唯一的,所以也不用BFS这么麻烦了,直接沿着临边遍历即可。

class Solution {
public:int closestMeetingNode(vector<int>& p, int x, int y) {int n = p.size();vector<int> d1(n, -1), d2(n, -1); // 存每个点到起点的距离// 初始化d1[x] = d2[y] = 0;while(p[x] != -1){   // 如果有环,则退出if(d1[p[x]] != -1) break;d1[p[x]] = d1[x] + 1;x = p[x];}while(p[y] != -1){if(d2[p[y]] != -1) break;d2[p[y]] = d2[y] + 1;y = p[y];}// 遍历所有节点int res = -1, id = -1;for(int i = 0; i < n; i ++){int a = d1[i], b = d2[i];if(a != -1 && b != -1){if(res == -1 || res > max(a, b)){res = max(a, b);id = i;}}}return id;}
};
class Solution:def closestMeetingNode(self, p: List[int], x: int, y: int) -> int:n = len(p)d1 = [-1] * nd2 = [-1] * n d1[x] = 0 d2[y] = 0while p[x] != -1:if d1[p[x]] != -1:breakd1[p[x]] = d1[x] + 1x = p[x]while p[y] != -1:if d2[p[y]] != -1:breakd2[p[y]] = d2[y] + 1y = p[y]# print(d1)# print(d2)res, id = -1, -1for i in range(n):a, b = d1[i], d2[i]if a != -1 and b != -1:if res == -1 or res > max(a, b):res, id = max(a, b), ireturn id

灵佬的代码:
在这里插入图片描述
最后一个循环的优化:

class Solution:def closestMeetingNode(self, edges: List[int], node1: int, node2: int) -> int:n, min_dis, ans = len(edges), len(edges), -1def calc_dis(x: int) -> List[int]:dis = [n] * nd = 0while x >= 0 and dis[x] == n:dis[x] = dd += 1x = edges[x]return disfor i, d1, d2 in zip(range(n), calc_dis(node1), calc_dis(node2)):d = max(d1, d2)if d < min_dis:min_dis, ans = d, ireturn ans作者:endlesscheng
链接:https://leetcode.cn/problems/find-closest-node-to-given-two-nodes/solution/ji-suan-dao-mei-ge-dian-de-ju-chi-python-gr2u/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

这篇关于LeetCode 2359. 找到离给定两个节点最近的节点 基环树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/378463

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

poj1330(LCA最近公共祖先)

题意:求最近公共祖先 思路:之前学习了树链剖分,然后我就用树链剖分的一小部分知识就可以解这个题目了,记录每个结点的fa和depth。然后查找时,每次将depth大的结点往上走直到x = y。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>

day-51 合并零之间的节点

思路 直接遍历链表即可,遇到val=0跳过,val非零则加在一起,最后返回即可 解题过程 返回链表可以有头结点,方便插入,返回head.next Code /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}*

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

两个月冲刺软考——访问位与修改位的题型(淘汰哪一页);内聚的类型;关于码制的知识点;地址映射的相关内容

1.访问位与修改位的题型(淘汰哪一页) 访问位:为1时表示在内存期间被访问过,为0时表示未被访问;修改位:为1时表示该页面自从被装入内存后被修改过,为0时表示未修改过。 置换页面时,最先置换访问位和修改位为00的,其次是01(没被访问但被修改过)的,之后是10(被访问了但没被修改过),最后是11。 2.内聚的类型 功能内聚:完成一个单一功能,各个部分协同工作,缺一不可。 顺序内聚:

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return