拓扑排序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. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  2. uva 1415 - Gauss Prime(高斯素数)

    题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=−2‾‾‾√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != ...

  3. UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)

    继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...

  4. c# listview导出excel文件

    首先在工程中需要添加对Microsoft Excel office 11.0 object的引用 生成excel的类代码如下 using System; using System.Collection ...

  5. 16位图像Alpha混合的实现(用汇编写的,比MMX还要快)

    Alpha 混合的算法很简单,基于下面的公式就可以实现: D := A * (S - D) / 255 + D D 是目标图像的像素, S 是源图像的像素 A 是 Alpha 值, 0 为全透明, 2 ...

  6. 14.4.1 InnoDB Startup Configuration

    14.4 InnoDB Configuration :InnoDB 配置: 14.4 InnoDB Configuration 14.4.1 InnoDB Startup Configuration ...

  7. freemark换行输出

    <!--附件图片-->              <#if attatList? exists>       <#if (attatList?size>0)> ...

  8. Android开源项目大全 - 工具类

    主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...

  9. POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)

    Bob’s Race Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 378   Accepted: 119 Descript ...

  10. AngularJS是为了克服HTML在构建应用上的不足而设计的

    AngularJS中文网:http://www.apjs.net/ 简介   AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构 ...