输入数据:

4 6
1 2
1 3
2 3
3 4
2 4
4 2

4 6
1 2
1 3
2 3
3 4
2 4
1 2

topo排序为偏序:

 #include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int indegree[] ;
queue<int>q;
int n , m ;
bool map[][] ;
int a[] ;
int topo(int n)
{
int cnt = ;
while (!q.empty ())
q.pop () ; for(int i = ; i <= n ; i++) {
if (indegree[i] == ) {
q.push(i);//入度为0的点为起始点
}
} int temp ;
while(!q.empty()) {
temp = q.front();
a[cnt ++] = temp ;
q.pop();
for(int i = ; i <= n ; i++) {
if(map[temp][i]) {
indegree[i]--;
if(indegree[i] == )
q.push(i);
}
}
}
if (cnt == n) //当输出的顶点数小于图中的顶点数时,输出有回路信息
for (int i = ; i < n ; i++) {
printf ("%d----->" , a[i]) ;
}
else
puts ("The network has a cycle!") ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
int u , v ;
while (~ scanf ("%d%d" , &n , &m)) {
memset (indegree , , sizeof(indegree)) ;
memset (map , , sizeof(map)) ;
for (int i = ; i < m ; i++) {
scanf ("%d%d" , &u , &v) ;
if (!map[u][v])//在u 和 v 没有连通时
indegree[v]++ ;//点v的入度++
map[u][v] = ;//表示连通
}
topo (n) ;
}
}

演示:http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/topological_sort.asp

详细:http://blog.csdn.net/dm_vincent/article/details/7714519

 -----------------hash table------------------------------------
struct Hash
{
int nxt ;
ll w ;
Hash () {}
Hash (int nxt , ll w) : nxt (nxt) , w (w) {}
}e[M];
int H[M] , E ; void init ()
{
E = ;
memset (H , , sizeof(H) ) ;
} void Insert (ll x)
{
int y = x % M ;
if (y < ) y += M ;
e[E ++] = (H[y] , x) ;
H[y] = E ;
} bool Find (ll x)
{
int y = x % M ;
if (y < ) y += M ;
for (int i = H[y] ; i ; i = e[i].nxt) {
if (e[i].w == x) return true ;
}
return false ;
}
------------------邻接表--------------------------------------------------------
struct edge
{
int u , v , w , nxt ;
edge () {}
edge (int u , int v , int w , int nxt) : u (u) , v (v) , w (w) , nxt (nxt) {}
}e[M];
int H[M] , E ;
void init ()
{
memset (H , , sizeof(H) ) ;
E = ;
} void addedge ()
{
e[E ] = edge (u , v , w , H[u]) ;
H[u] = E ++;
e[E ] = edge (v , u , w , H[v]) ;
H[v] = E ++;
}

用邻接表优化后的。时间,空间复杂度都降为O(N)

topo排序 + 用邻接表优化后的的更多相关文章

  1. 【数据结构】【图文】【oj习题】 图的拓扑排序(邻接表)

    拓扑排序: 按照有向图给出的次序关系,将图中顶点排成一个线性序列,对于有向图中没有限定次序关系的顶点,则可以人为加上任意的次序关系,由此所得顶点的线性序列称之为拓扑有序序列.显然对于有回路的有向图得不 ...

  2. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  3. poj2002 hash+邻接表优化Squares

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17487   Accepted: 6643 Descript ...

  4. hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

    DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  5. ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)

    做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...

  6. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

  7. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

  8. 图论——最小生成树prim+邻接表+堆优化

    今天学长对比了最小生成树最快速的求法不管是稠密图还是稀疏图,prim+邻接表+堆优化都能得到一个很不错的速度,所以参考学长的代码打出了下列代码,make_pair还不是很会,大体理解的意思是可以同时绑 ...

  9. HUST 1103 校赛 邻接表-拓扑排序

    Description N students were invited to attend a party, every student has some friends, only if someo ...

随机推荐

  1. 在线富文本编辑器kindeditor配置(.Net Framework 3.5)

    下载地址:http://kindeditor.net/down.php 解压放在项目要目录下, 在Bin目录下添加引用:LitJSON.dll 在页面头部加: <link rel="s ...

  2. angular-scope.assign

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 【web必知必会】—— 图解HTTP(下)

    上一篇<图解HTTP 上>总结了HTTP的报文格式,发送方式,以及HTTP的一些使用. 本文再总结以下内容: 1 http状态码 2 http报文首部中的各字段 3 http中的身份验证 ...

  4. java.lang.NoClassDefFoundError: org/objectweb/asm/Type

    Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/ ...

  5. jQuery1.4源码解读

    来吧, 慢慢折腾吧 总结一下: jq1.4挺简单的, 正则写的不多, 看的都懂, 多写一些 三目写法到底要不要 特殊的地方的注释一定要有 /*! * jQuery JavaScript Library ...

  6. myeclipse6.0 序列号生成器源码

    import java.io.*; public class Test{ private static final String LL = "Decompiling this copyrig ...

  7. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...

  8. str内部方法

    代码 #str内部功能 name=' aK am\til.L iu' age=18 num=-11 ab='#' ac=('1','2','3','4','5','6','7') print(dir( ...

  9. 42.Android之ListView中ArrayAdapter简单学习

    今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...

  10. 【poj1088】 滑雪

    http://poj.org/problem?id=1088 (题目链接) 题意 给出一个矩阵,任意选择一个起点,每次只能向周围4个格子中的值比当前格子小的格子移动,求最多能移动多少步. Soluti ...