Leetcode 797. 所有可能的路径 图DFS

news/2024/7/20 20:16:49 标签: leetcode, 算法, 深度优先

原题链接:Leetcode 797. 所有可能的路径

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<vector<int>> res;
    void dfs(vector<vector<int>>& g,int x,vector<int>& tmp)
    {
        if(x==0) 
        {
            vector<int> r(tmp);
            reverse(r.begin(),r.end());
            res.push_back(r);
        }
        for(auto num:g[x])
        {
            tmp.push_back(num);
            dfs(g,num,tmp);
            tmp.pop_back();
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        int n=graph.size();
        vector<vector<int>> g(n);
        for(int i=0;i<n;i++)
        {
            for(auto x:graph[i]) g[x].push_back(i);
        }
        vector<int> tmp;
        tmp.push_back(n-1);
        dfs(g,n-1,tmp);
        return res;
    }
};
class Solution {
public:
    vector<vector<int>> res;
    vector<int> tmp;
    void dfs(vector<vector<int>>& graph,int x,int n)
    {
        if(x==n) res.push_back(tmp);
        for(auto num:graph[x])
        {
            tmp.push_back(num);
            dfs(graph,num,n);
            tmp.pop_back();
        }
    }
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        int n=graph.size();
        tmp.push_back(0);
        dfs(graph,0,n-1);
        return res;
    }
};

http://www.niftyadmin.cn/n/1264524.html

相关文章

Leetcode 547. 省份数量 求图的连通分量

原题链接&#xff1a;Leetcode 547. 省份数量 DFS class Solution { public:vector<int> visited;void dfs(vector<vector<int>>& isConnected,int x,int n){for(int i0;i<n;i){if(!visited[i] && isConnected[i][x]){visited[i]1;dfs(isCon…

Leetcode 1319. 连通网络的操作次数 DFS/并查集

原题链接&#xff1a;Leetcode 1319. 连通网络的操作次数 并查集路径压缩 class Solution { public:map<int,int> fa;int findfather(int x){int ax;while(x!fa[x]) xfa[x];while(a!fa[a]){int zfa[a];afa[a];fa[z]x;}return x;}void Union(int x,int y){int fxfindfa…

Leetcode 743. 网络延迟时间 DFS/BFS/Dijkstra/Dijkstra+优先队列/Floyd/Bellman-Ford

原题链接&#xff1a;Leetcode 743. 网络延迟时间 DFS class Solution { public:vector<vector<pair<int,int>>> adj;vector<int> visit;void dfs(int now,int w){if(visit[now]>w){visit[now]w;for(auto x:adj[now]){int ax.first,bx.second;df…

对于互信息的理解

转载&#xff1a;互信息&#xff08;Mutual Information&#xff09;的介绍

Leetcode 310. 最小高度树 BFS

原题链接&#xff1a;Leetcode 310. 最小高度树 超时DFS&#xff1a; class Solution { public:int res0;void dfs(vector<int>& visit,vector<vector<int>>& edges,int now,int h){visit[now]1;int flag-1;for(auto x:edges){int ax[0],bx[1];if(a…

Leetcode 841. 钥匙和房间

原题链接&#xff1a;Leetcode 841. 钥匙和房间 DFS class Solution { public:vector<int> visit;vector<vector<int>> adj;int num0;void dfs(int i){visit[i]1;num;for(auto x:adj[i]){if(!visit[x]) dfs(x);}}bool canVisitAllRooms(vector<vector<…

Leetcode 851. 喧闹和富有 DFS/拓扑排序

原题链接&#xff1a;Leetcode 851. 喧闹和富有 DFS1&#xff1a; class Solution { public:int tmpINT_MAX;int person;vector<vector<int>> adj;vector<int> visit;void dfs(vector<int>& quiet,int now){if(quiet[now]<tmp){tmpquiet[now];…

Leetcode 1042. 不邻接植花 图染色

原题链接&#xff1a;Leetcode 1042. 不邻接植花 class Solution { public:vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {vector<int> res(n1);vector<vector<int>> adj(n1);for(auto x:paths){int ax[0],bx[1];adj…