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 ...
随机推荐
- CentOS6.8 安装FTP及添加用户
一 安装FTP 1 检测是否已经安装FTP rpm -qa | grep vsftpd 2 若没有,则进行安装 yum install vsftpd 二 设置vsftpd开机启动 chkconfig ...
- 基于JWT的Token开发案例
代码地址如下:http://www.demodashi.com/demo/12531.html 0.准备工作 0-1运行环境 jdk1.8 maven 一个能支持以上两者的代码编辑器,作者使用的是ID ...
- Android轻量级日志管理框架
代码地址如下:http://www.demodashi.com/demo/12134.html ViseLog Android 轻量级日志框架,使用森林对象维护不同的日志树进行日志输出,可以是Logc ...
- Atitit.异步编程的发展历史 1.1. TAP & async/await
Atitit.异步编程的发展历史 1.1. TAP & async/await 1. 异步编程的发展历史1 1.1. Thread1 1.2. Task1 1.3. Async await2 ...
- How to Use HTML5 FUll Screen API(怎样使用HTML5全屏接口)
原文链接:http://www.sitepoint.com/use-html5-full-screen-api/ 假设你不太喜欢变化太快的东西,那么web开发可能不适合你. 我曾在2012年末有写过F ...
- kafka 小案例【二】 --kafka 设置多个消费着集群
这个配是我在http://www.cnblogs.com/zhangXingSheng/p/6646972.html 的基础上再添加的配置 设置多个消息集群 (1)复制两份配置文件 > cp c ...
- excel批量取消隐藏工作表
按下"Alt+F11"键,在打开的"Microsoft Bisual Basic"窗口中,选择"插入——模块".,复制下面的代码,按F5键运 ...
- 怎样查看Eclipse是32位还是64位?
怎样查看Eclipse是32位还是64位? 1.去Eclipse的安装文件夹,找到eclipse.ini 2.打开这个文件.寻找:launcher.library,我的机器上,在第二行 3.查看&qu ...
- PHP性能:序——谈ab(Apache Bench)压力测试工具
PHP性能:序——谈ab(Apache Bench)压力测试工具 ab(Apache Bench)是啥? ab是Apache自带的一个压力测试软件,可以通过ab命令和选项对某个URL进行压力测试.a ...
- MDwiki 调研
下载解压 然后 mv mdwiki.html index.html 创建 一个index.md 然后就可以直接访问http://example.com/ 直接把你的md文件放在目录里面就可以了 cer ...