当前位置: 首页 > article >正文

代码随想录算法训练营day50

1.所有可达路径

1.1 题目

https://kamacoder.com/problempage.php?pid=1170

1.2 题解

#include <iostream>
#include <vector>
#include <list>
using namespace std;


//收集结果数组
vector<vector<int>> result;
//单个路径
vector<int> path;
//递归函数,x代表当前遍历的节点,n代表终点节点
void dfs(vector<vector<int>>& graph, int x, int n)
{
    //确定终止条件
    if (x == n)
    {
        result.push_back(path);
        return;
    }
    //遍历节点x连接的所有节点
    for (int i = 1; i <= n; i++)
    {
        if (graph[x][i] == 1)
        {
            path.push_back(i);
            dfs(graph, i, n);
            path.pop_back();
        }
    }

}


int main()
{


    int nodes;
    int margins;
    cin >> nodes >> margins;
    int s;
    int t;
    //构造邻接矩阵
    vector<vector<int>> graph(nodes + 1, vector<int>(nodes + 1, 0));
    for (int i = 0; i < margins; ++i)
    {
        cin >> s >> t;
        //存储
        graph[s][t] = 1;
    }
    path.push_back(1);
    dfs(graph, 1, nodes);
    // 输出结果
    if (result.size() == 0) cout << -1 << endl;
    for (const vector<int>& pa : result) {
        for (int i = 0; i < pa.size() - 1; i++) {
            cout << pa[i] << " ";
        }
        cout << pa[pa.size() - 1] << endl;
    }
}

http://www.kler.cn/news/337629.html

相关文章:

  • webpack信息泄露
  • 软考高级之系统架构师之计算机硬件基础
  • Leetcode: 0001-0010题速览
  • 【rCore OS 开源操作系统】Rust HashMap应用 知识点及练习题
  • ES(Elasticsearch)SSL集群部署
  • slurm上使用jupyter
  • uniapp生成随机数
  • 2-112基于matlab的协同干扰功率分配模型
  • FWA(固定无线接入),CPE(客户终端设备)简介
  • VADv2 论文学习
  • 【12月IEEE出版* 镇江 】第九届清洁能源与发电技术国际学术会议(CEPGT 2024)
  • EDA脚本应用领域及使用特点
  • 【重学 MySQL】四十三、多行子查询
  • 踩坑spring cloud gateway /actuator/gateway/refresh不生效
  • Ubuntu开机进入紧急模式处理
  • 工业数采的常用通讯协议
  • LeetCode题练习与总结:寻找重复数--287
  • Spring Boot实现的医院资源优化工具
  • 5G NR 切换流程简介
  • 高效医疗:Spring Boot医院管理解决方案