Leetcode: 1971. Find if Path Exists in Graph

思路

用dps从start点走一遍,然後检查end点有没有finish。

程序码

class Solution {
public:
    bool validPath(int n, vector<vector<int>>& edges, int start, int end) {
        vector<vector<int>> adjlist(n);
        for(auto el: edges) {
            adjlist[el[1]].push_back(el[0]);
            adjlist[el[0]].push_back(el[1]);
        } 
        
        vector<bool> discover(n, false), finish(n, false);
        
    
        int e = dps(adjlist, discover, finish, start);
        if (finish[end])
            return true;
        else
            return false;
    }
private:
    int dps(vector<vector<int>>& adjlist, vector<bool>& discover, vector<bool>&finish, int curr_node) {
        if (discover[curr_node])
            return 0;
        if (finish[curr_node])
            return 0;
        
        discover[curr_node] = true;
        for(auto el: adjlist[curr_node])
            int e = dps(adjlist, discover, finish, el);

        discover[curr_node] = false; finish[curr_node] = true;
        return 0;
        
    }
};

过程中

遇到

AddressSanitizer:DEADLYSIGNAL
=================================================================
==31==ERROR: AddressSanitizer: stack-overflow on address 0x7ffc4183cfe0 (pc 0x0000003461c1 bp 0x7ffc4183d090 sp 0x7ffc4183cfc0 T0)
==31==ABORTING

这个是递回太深造成的,递回终止条件没写好的关系

参考:
https://ithelp.ithome.com.tw/articles/10268205
https://leetcode.com/problems/find-if-path-exists-in-graph/
https://web.ntnu.edu.tw/~algo/Graph.html#7


<<:  D24 第九周 php 留言板作业的心得之二

>>:  小公司不是一块跳板,小公司本身就是一个伟大的目标

DAY8 Linebot 自动回应-1

设定完成後,开启Django应用程序(APP)的views.py档案,这边就是撰写LINE Bot接...

【DAY 4】 Power Automate 简介 + 订便当系统

哈罗大家好~ 今天要简单说明 “ Power Automate “ 这个强大的流程引擎以及示范一个用...

Day 14:凯撒密码之Shifting Letters

在开始今天题目之前,先来认识一下凯撒密码 (Caesar cipher) 凯撒密码是一种替换加密技术...

Day20 - 铁人付外挂设定介面(二)- 全域设定

对於 WordPress 资料库结构有个大概的认识後,我们就能把後台的设定选项归纳为两种,一种是放在...

C# web Form web.aspx 跳出提示视窗的4种方法

一般在写ASP.NET是不太希望用 response.write来作页面输出。 因为用respon...