拓扑排序O(E), bellman O(VE)   , 使用邻接表的dfs O(V+E) ,floyd O(N*N*N)

bellman算法只能判断是否存在负环。

所以可以先把权值全部设为-1

 #include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
using namespace std;
const int N = + ;
const int INF = <<;
struct node
{
int u,v,weight;
}g[N+N];
int dist[N];
inline int max(const int &a, const int &b)
{
return a < b ? b : a;
} void init(int n)
{
for(int i=; i<=n; ++i)
{ dist[i] = INF;
}
} void relax(int u, int v, int weight)
{
if(dist[v] > dist[u] + weight)
dist[v] = dist[u] + weight;
}
bool bell(int n, int m)
{
int i,j;
for(i=; i<n; ++i)
for(j=; j<m; ++j)
relax(g[j].u,g[j].v,g[j].weight); for(i=; i<m; ++i)
if(dist[g[i].v] > dist[g[i].u] +g[i].weight)
return true;
return false;
}
int main()
{
int n,m,i,u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n== && m==)
break;
init(n);
for(i=; i<m; ++i)
{
scanf("%d%d",&g[i].u,&g[i].v);
g[i].weight = -;//把权值全部改为负的,然后判断是不是存在负环
if(g[i].u==)
dist[g[i].v] = -;
}
if(bell(n,m))
puts("NO");
else
puts("YES");
}
}

枚举起点进行dfs,如果能遇到顶点和起点相同,则存在环

 #include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
using namespace std;
const int N = + ;
vector<int> g[N];
int start;
bool vis[N],flag;
void dfs(int u)
{
if(flag)
return;
for(int i=;i<g[u].size(); ++i)
{
int v = g[u][i];
if(v==start)
{
flag = true;//有环
return;
}
if(!vis[v])
{
vis[v] = true;
dfs(v);
}
}
}
int main()
{
int n,m,i,u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n== && m==)
break;
for(i=; i<n; ++i)
g[i].clear();
for(i=; i<m; ++i)
{
scanf("%d%d",&u,&v);
g[u].push_back(v);
}
flag = false;
for(i=; i<n; ++i)
{
memset(vis,,sizeof(vis));
vis[i] = true;
start = i;
dfs(i);
if(flag)
break;
}
if(flag)
puts("NO");
else
puts("YES");
}
}

拓扑排序如果能生成n个顶点序列,那么说明是GAG图

 #include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
using namespace std;
const int N = + ;
vector<int> g[N];
stack<int> st;
int in[N],add[N],cnt;
inline int max(const int &a, const int &b)
{
return a < b ? b : a;
}
void topSort(int n, int m)
{
int u,i;
for(i=; i<=n; ++i)
if(in[i]==)
st.push(i);
while(!st.empty())
{
u = st.top();
cnt++;
st.pop();
for(i=; i<g[u].size(); ++i)
{
--in[g[u][i]];
if(in[g[u][i]]==)
st.push(g[u][i]); }
}
}
void init(int n)
{
cnt = ;
for(int i=; i<=n; ++i)
{
g[i].clear();
add[i] = in[i] = ;
}
}
int main()
{
int n,m,i,u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n== && m==)
break;
init(n);
for(i=; i<m; ++i)
{
scanf("%d%d",&u,&v);
u++;
v++;
g[u].push_back(v);
in[v]++;
}
topSort(n,m);
if(cnt==n)//能生成n个顶点的序列,说明是DAG图
{
puts("YES");
}
else
puts("NO");
}
}

判断DAG图的更多相关文章

  1. 图论--拓扑排序--判断是否为DAG图

    #include<cstdio> #include<cstring> #include<vector> #include<queue> using na ...

  2. POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)

    (点击此处查看原题) 题意 给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权 ...

  3. 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))

    sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  给定一个有向图 ...

  4. 有标号的DAG图计数1~4

    前言 我什么都不会,菜的被关了起来. 有标号的DAG图I Solution 考虑递推,设\(f_i\)表示i个点的答案,显然这个东西是可以组合数+容斥递推? 设\(f_i\)表示i个点的答案,我们考虑 ...

  5. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  6. Tarjan缩点+DAG图dp

    题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...

  7. 《编译原理》画 DAG 图与求优化后的 4 元式代码- 例题解析

    <编译原理>画 DAG 图与求优化后的 4 元式代码- 例题解析 DAG 图(Directed Acylic Graph)无环路有向图 (一)基本块 基本块是指程序中一顺序执行的语句序列, ...

  8. 洛谷 P2656 (缩点 + DAG图上DP)

    ### 洛谷 P2656 题目链接 ### 题目大意: 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖 ...

  9. SDUT-2140_判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个有向图,判 ...

随机推荐

  1. 内网穿透神器ngrok(转)

    相信做Web开发的同学们,经常会遇到需要将本地部署的Web应用能够让公网环境直接访问到的情况,例如微信应用调试.支付宝接口调试等.这个时候,一个叫ngrok的神器可能会帮到你,它提供了一个能够在公网安 ...

  2. 自适应滤波器(Adaptive Filter)

    ======= Wikipedia的解释 ======= 自适应滤波器是能够根据输入信号自动调整性能进行数字信号处理的数字滤波器.作为对比,非自适应滤波器有静态的滤波器系数,这些静态系数一起组成传递函 ...

  3. C++内存管理学习笔记(5)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  4. MySQL 存储过程例子,不能在if else里面用begin end否则会报错Error Code : 1064!

    Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your My ...

  5. Swift - 选择框(UIPickerView)的用法

    1,选择框可以让用户以滑动的方式选择值.示例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  6. Ajax 下拉列表联动显示

    一般处理程序文件 代码 using System;using System.Web;using System.Linq;using System.Data.Linq;using System.Text ...

  7. 新的方法 (New Approach)¶

    第一章:简介 - ANSI Common Lisp 中文版 新的方法 (New Approach)¶ 本书的目标之一是不仅是教授 Lisp 语言,而是教授一种新的编程方法,这种方法因为有了 Lisp ...

  8. OCP读书笔记(2) - 配置恢复

    RMAN的命令类型 1. sqlplus命令 [oracle@oracle admin]$ export ORACLE_SID=orcl [oracle@oracle admin]$ rman tar ...

  9. ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版

    ANSI Common Lisp 中文翻譯版 — ANSI Common Lisp 中文版 ANSI Common Lisp 中文翻譯版¶

  10. Android 检測网络是否连接

    权限: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  <u ...