Data Structure Graph: cycle in a directed graph
geeks上的解答复杂了些,用回溯就行了
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
using namespace std; const int N = ;
vector<int> trace;
vector<bool> visit(N);
int graph[N][N] = {{, , , , },
{, , , , },
{, , , , },
{, , , , },
{, , , , }}; void cycle(int v) {
if (visit[v]) {
int beg = ;
for (; beg < trace.size(); ++beg)
if (trace[beg] == v) break;
for (int i = beg; i < trace.size(); i++) {
cout << trace[i] << " ";
}
cout << endl;
return;
}
visit[v] = true;
trace.push_back(v);
for (int i = ; i < N; i++) {
if (graph[v][i]) cycle(i);
}
trace.pop_back();
visit[v] = false;
} int main() {
cycle();
return ;
}
Data Structure Graph: cycle in a directed graph的更多相关文章
- Geeks - Detect Cycle in a Directed Graph 推断图是否有环
Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...
- Detect cycle in a directed graph
Question: Detect cycle in a directed graph Answer: Depth First Traversal can be used to detect cycle ...
- [Algorithm] JavaScript Graph Data Structure
A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...
- Data Structure Graph: strong connectivity
如果为undirected graph就是dfs或者bfs,如果都能visit则为连通O(V+E). 如果为directed graph就先dfs或者bfs,再reverse direct,再dfs或 ...
- dataStructure@ Find if there is a path between two vertices in a directed graph
Given a Directed Graph and two vertices in it, check whether there is a path from the first given ve ...
- CodeChef Counting on a directed graph
Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...
- [CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nod ...
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- Directed Graph Loop detection and if not have, path to print all path.
这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...
随机推荐
- mysql :安装过程中的问题
安装mysql 选择路径的问题 出现如下问题:说明之前安装过mysql,写在的不干净
- .Net 异步调用
.NET异步编程之新利器——Task与Await.Async 一. FrameWork 4.0之前的线程世界 在.NET FrameWork 4.0之前,如果我们使用线程.一般有以下几种方 ...
- WCF实现上传图片功能
初次学习实现WCF winform程序的通信,主要功能是实现图片的传输. 下面是实现步骤: 第一步: 首先建立一个类库项目TransferPicLib,导入wcf需要的引用System.Service ...
- Wd 西部数据
西部数据 https://item.jd.com/3564471.html#none 打算买一个大硬盘记录代码片段.开发项目.开发工具.电影游戏等…… /** * 获取100天后的日子 * 用来做计划 ...
- IntelliJ IDEA(2017)下载并破解
idea激活,JetBrain旗下软件激活 我在修改这个博主的文章再添加了code码 http://blog.csdn.net/qq_24504453/article/details/77407329 ...
- 关于ViewData,ViewBag,TempData三者学习记录!
关于ViewData,ViewBag,TempData三者学习分享! 1.ViewData和TempData是字典类型,赋值方式用字典方式,ViewData["Key"] . 2. ...
- k8s部署dns
硬件环境: 两台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-schedule ...
- win10正式版开始菜单无法打开,右边的网络连接、操作中心也打不开
问题描述: 开机后电脑键盘的win键无响应,鼠标点击菜单栏中的这几个按键也都无响应,但是点击自己固定的应用程序却没有问题,在网上查找尝试了许多资料,终于找到了一个合适的解决方案.现记录如下 解决方案: ...
- Android数据适配器(Adapter)优化:使用高效的ViewHolder
原文链接:http://stackvoid.com/using-adapter-in-efficiency-way/ 在使用Listview或GridView的时候,往往须要自己定义数据适配器.一般都 ...
- mac地址绑定
1.导入第三方类库: <?php /** * FILE_NAME : Macaddr.php * linux平台获取服务器mac地址 * @filesource */ class Macaddr ...