首页 > 其他分享 >Leetcode 1971. 寻找图中是否存在路径

Leetcode 1971. 寻找图中是否存在路径

时间:2024-05-27 13:02:13浏览次数:14  
标签:int destination 1971 find source edges 图中 顶点 Leetcode

有一个具有 n 个顶点的 双向 图,其中每个顶点标记从 0 到 n - 1(包含 0 和 n - 1)。图中的边用一个二维整数数组 edges 表示,其中 edges[i] = [ui, vi] 表示顶点 ui 和顶点 vi 之间的双向边。 每个顶点对由 最多一条 边连接,并且没有顶点存在与自身相连的边。

请你确定是否存在从顶点 source 开始,到顶点 destination 结束的 有效路径 。

给你数组 edges 和整数 n、source 和 destination,如果从 source 到 destination 存在 有效路径 ,则返回 true,否则返回 false 。

示例 1:
在这里插入图片描述

输入:n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
输出:true
解释:存在由顶点 0 到顶点 2 的路径:

  • 0 → 1 → 2
  • 0 → 2
    示例 2:
    在这里插入图片描述

输入:n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
输出:false
解释:不存在由顶点 0 到顶点 5 的路径.

提示:

1 <= n <= 2 * 105
0 <= edges.length <= 2 * 105
edges[i].length == 2
0 <= ui, vi <= n - 1
ui != vi
0 <= source, destination <= n - 1
不存在重复边
不存在指向顶点自身的边
思路:并查集。

class Solution {
public:
    static const int N = 2 * 100010;
    int p[N];

    int find(int x) {
        if(p[x] != x) p[x] = find(p[x]);
        return p[x]; 
    }

    bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
        for(int i = 0; i < n; i ++ ) p[i] = i;
        
        for(int i = 0; i < edges.size(); i ++ ) {
            int a = edges[i][0], b = edges[i][1];
            if(find(a) != find(b)) p[find(a)] = find(b);
        }

        return find(destination) == find(source);
    }
};

标签:int,destination,1971,find,source,edges,图中,顶点,Leetcode
From: https://blog.csdn.net/qq_45281807/article/details/139232233

相关文章

  • Leetcode 684. 冗余连接
    树可以看成是一个连通且无环的无向图。给定往一棵n个节点(节点值1~n)的树中添加一条边后的图。添加的边的两个顶点包含在1到n中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为n的二维数组edges,edges[i]=[ai,bi]表示图中在ai和bi之间......
  • LeetCode刷题之HOT100之两数相加
    2024/5/27大家早上好呀,昨晚没睡好,四个小时不到,估计是太兴奋了。昨天去长乐十七孔、下沙赶海啦。远看沙滩上的人群就像一根根木桩矗立在浅滩上,走近些,才发现都佝偻着腰,两只手在沙地淘金(摸花蛤)。放几张图图一、十七孔水库附近图二、十七孔——右侧礁石是妈祖像图三、追......
  • Leetcode1953. 你可以工作的最大周数
    EverydayaLeetcode题目来源:1953.你可以工作的最大周数类似题目:621.任务调度器解法1:贪心本质上来说,我们需要构造一个尽量长的,相邻元素不同的序列,且元素x的出现次数不能超过milestones[x]。设milestones的元素和为s,这是序列长度的上界。设mx=max⁡(milestone......
  • leetcode力扣 300. 最长递增子序列
    给你一个整数数组nums,找到其中最长严格递增子序列的长度。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7]是数组[0,3,1,6,2,2,7]的子序列。示例1:输入:nums=[10,9,2,5,3,7,101,18]输出:4解释:最长递增子序列是[2,3,7,101......
  • 【leetcode 找出第 K 大的异或坐标值]
    前缀和+最小堆importjava.util.PriorityQueue;classSolution{publicstaticvoidmain(String[]args){Solutionsolution=newSolution();solution.kthLargestValue(newint[][]{{5,2},{1,6}},4);}......
  • 【leetcode 399 周赛】【题解】
    第一题和第三题一样。就是求约数第二题就是模拟第4题使用线段树1,3题代码实际上发现没有下面代码的负载,比如:a*b=n,枚举a就好,a在[1,sqrt(n)内。importjava.util.*;classSolution{publicintnumberOfPairs(int[]nums1,int[]nums2,intk){......
  • 【Leetcode 每日一题】28. 找出字符串中第一个匹配项的下标
    给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从0开始)。如果 needle 不是 haystack 的一部分,则返回  -1 。示例1:输入:haystack="sadbutsad",needle="sad"输出:0解释:"sad"在下标0和6......
  • leetcode力扣 1004. 最大连续1的个数 III
    给定一个二进制数组nums和一个整数k,如果可以翻转最多k个0,则返回数组中连续1的最大个数。示例1:输入:nums=[1,1,1,0,0,0,1,1,1,1,0],k=2输出:6解释:[1,1,1,0,0,1,1,1,1,1,1],翻转两个0后,最长的子数组长度为6。示例2:输入:nums=[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1......
  • 地下城游戏(leetcode)
    个人主页:Lei宝啊 愿所有美好如期而遇地下城游戏https://leetcode.cn/problems/dungeon-game/description/图解+分析:代码classSolution{public:intcalculateMinimumHP(vector<vector<int>>&vv){introw=vv.size(),col=vv[0].size();......
  • [LeetCode] 2903. Find Indices With Index and Value Difference I
    Youaregivena0-indexedintegerarraynumshavinglengthn,anintegerindexDifference,andanintegervalueDifference.Yourtaskistofindtwoindicesiandj,bothintherange[0,n-1],thatsatisfythefollowingconditions:abs(i-j)>=index......