#include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<vector>
using namespace std;
const int maxn = ;
struct edge{
int to, cost;
edge(int t){
this->to = t; this->cost = ;
}
};
void addEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
edgelist.push_back(edge(to));
G[from].push_back(edgelist.size()-);
}
void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
addEdge(edgelist,G,from,to);
addEdge(edgelist,G,to,from);
}
bool isCyclic(vector<edge> edgelist, vector<vector<int> > G,vector<bool> vis, vector<bool> RecStack, int v){
for(int i=;i<G[v].size();++i){
edge e = edgelist[G[v][i]];
if(RecStack[e.to]) return true;
if(!vis[e.to]){
vis[e.to] = true; RecStack[e.to] = true;
if(isCyclic(edgelist,G,vis,RecStack,e.to)) return true;
RecStack[e.to] = false;
}
}
return false;
}
void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
}
int main(){
vector<edge> edgelist;
vector<vector<int> > G(maxn);
vector<bool> vis(maxn);
vector<bool> RecStack(maxn); buildMap(edgelist,G); for(int i=;i<vis.size();++i) vis[i]=false;
for(int i=;i<RecStack.size();++i) RecStack[i]=false; for(int i=;i<G.size();++i){
if(!vis[i]){
vis[i] = true; RecStack[i] = true;
if(isCyclic(edgelist,G,vis,RecStack,i)){
cout<<i<<" starts a cycle"<<endl;
}
RecStack[i] = false;
}
} return ;
}

dataStructure@ Check if a directed graph has cycles的更多相关文章

  1. dataStructure@ Check whether a given graph is Bipartite or not

    Check whether a given graph is Bipartite or not A Bipartite Graph is a graph whose vertices can be d ...

  2. 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 ...

  3. Directed Graph Loop detection and if not have, path to print all path.

    这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Sched ...

  4. [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 ...

  5. [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 ...

  6. CodeChef Counting on a directed graph

    Counting on a directed graph Problem Code: GRAPHCNT All submissions for this problem are available. ...

  7. Geeks - Detect Cycle in a Directed Graph 推断图是否有环

    Detect Cycle in a Directed Graph 推断一个图是否有环,有环图例如以下: 这里唯一注意的就是,这是个有向图, 边组成一个环,不一定成环,由于方向能够不一致. 这里就是添加 ...

  8. Skeleton-Based Action Recognition with Directed Graph Neural Network

    Skeleton-Based Action Recognition with Directed Graph Neural Network 摘要 因为骨架信息可以鲁棒地适应动态环境和复杂的背景,所以经常 ...

  9. Find the Weak Connected Component in the Directed Graph

    Description Find the number Weak Connected Component in the directed graph. Each node in the graph c ...

随机推荐

  1. Spark中shuffle的触发和调度

    Spark中的shuffle是在干嘛? Shuffle在Spark中即是把父RDD中的KV对按照Key重新分区,从而得到一个新的RDD.也就是说原本同属于父RDD同一个分区的数据需要进入到子RDD的不 ...

  2. JVM并发机制的探讨——内存模型、内存可见性和指令重排序

    并发本来就是个有意思的问题,尤其是现在又流行这么一句话:“高帅富加机器,穷矮搓搞优化”. 从这句话可以看到,无论是高帅富还是穷矮搓都需要深入理解并发编程,高帅富加多了机器,需要协调多台机器或者多个CP ...

  3. undefined与null的区别---js

    不错... http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html

  4. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  5. C++中的INL

    inl文件介绍 inl文件是内联函数的源文件.内联函数通常在C++头文件中实现,但是当C++头文件中内联函数过多的情况下,我们想使头文件看起来简洁点,能不能像普通函数那样将内联函数声明和函数定义放在头 ...

  6. Cygwin安装时,选择163的源后出错:Unable to get setup.ini from <http://mirrors.163.com/cygwin/>

    [问题] 折腾: [记录]Cygwin下把make从v3.82换成v3.81 期间,选择了163的源,结果出错: Cygwin Setup Unable to get setup.ini from & ...

  7. 关于C的一些理解

    关于字符数组和字符指针 关于相互赋值问题一只有疑问,其实是自己搞不清指针和地址的关系.地址可以指向一块内存但是不一定存在于内存,比如字符数组名,数组名是地址,但是不实际存在于内存中,无法修改,而字符指 ...

  8. linux编译注解

    Linux kernel release 3.x <http://kernel.org/> These are the release notes for Linux version 3. ...

  9. bash 统计文件行数

    #假设文件名是:fortest.gtf declare -i fileLinesfileLines=`sed -n '$=' fortest.gtf`echo $fileLines #-------- ...

  10. grunt安装中的cli和--save-dev解释

    grunt官网安装grunt的介绍中是这么写的: npm install -g grunt-cli 这里安装Grunt的命令行支持(command line interface,简称CLI),在这之后 ...