图结构练习——判断给定图是否存在合法拓扑序列
2015-08-14 16:25
323 查看
题目描述
给定一个有向图,判断该有向图是否存在一个合法的拓扑序列。输入
输入包含多组,每组格式如下。第一行包含两个整数n,m,分别代表该有向图的顶点数和边数。(n<=10)
后面m行每行两个整数a b,表示从a到b有一条有向边。
输出
若给定有向图存在合法拓扑序列,则输出YES;否则输出NO。示例输入
1 0 2 2 1 2 2 1
示例输出
YES NO
提示
对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(TopoiSicai Order)的序列,简称拓扑序列。
把所有入度为0的点入队列,依次弹出,若某一点弹出,则把该点到达大下一个点的入度-1,在遍历一下若有入度为0的点继续入队列,重复上个操作,若该循环结束,假如弹出点为n个则 是拓扑,否则,不是。
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }*head[20]; int degree[20]; int st[20]; int dis[20]; int main() { int i,a,b,cnt; int n,m; struct node *p; for( i = 0; i < 20; i++ ) { head[i] = (struct node *)malloc(sizeof(struct node)); } while( ~scanf("%d %d",&n,&m) ) { int top = 0; cnt = 0; for( i = 0; i <= n; i++ ) { head[i]->next = NULL; } memset(degree,0,sizeof(degree)); memset(dis,0,sizeof(dis)); for( i = 0; i < m; i++ ) { scanf("%d %d",&a,&b); p = (struct node *)malloc(sizeof(struct node)); p->data = b; p->next = head[a]->next; head[a]->next = p; degree[b]++; } for( i = 1; i <= n; i++ ) { if( degree[i] == 0 ) { dis[i] = 1; st[++top] = i; } } while( top ) { int t = st[top--]; cnt++; p = head[t]->next; while( p ) { degree[p->data]--; p = p->next; } for( i = 1; i <= n; i++ ) { if( !dis[i] && degree[i] == 0 ) { dis[i] = 1; st[++top] = i; } } } if( cnt < n ) { printf("NO\n"); } else { printf("YES\n"); } } return 0; }
相关文章推荐
- TOMCAT修改jvm内存的方法
- IOS 编程初体验 第一篇:自学和培训的选择
- android linearlayout代替listview实现
- DF Engine Export 无法到处到数据库
- c++线程的创建
- CString类型的字符串写入文件,所遇到的问题
- 为何Windows下的动态库总伴随一个静态库?
- MDK 生成BIN文件 最简单方式
- HttpURLConnection和HttpClient两种连接方式的区别
- 数字信封流程图
- 如何在Linux下写无线网卡的驱动【转】
- C/C++中函数参数传递详解(一)
- LeetCode:Kth Smallest Element in a BST
- 判断素数——Dirichlet's Theorem on Arithmetic Progressions
- MFC 如何在项目中定义宏定义
- CodeForces 245C Game with Coins
- Apache Shiro 使用手册(一)Shiro架构介绍 - kdboy - ITeye技术网站
- excel 补全所有空格
- 递归反转一个栈
- OS X Lion版 如果我忘记了我的账户密码 我该怎么办?