拓扑排序-邻接矩阵表示
2017-12-18 17:53
357 查看
数据结构实验之图论十:判断给定图是否存在合法拓扑序列
Time Limit: 1000MSMemory Limit: 65536KB
Submit
Statistic
Discuss
Problem Description
给定一个有向图,判断该有向图是否存在一个合法的拓扑序列。Input
输入包含多组,每组格式如下。第一行包含两个整数n,m,分别代表该有向图的顶点数和边数。(n<=10)
后面m行每行两个整数a b,表示从a到b有一条有向边。
Output
若给定有向图存在合法拓扑序列,则输出YES;否则输出NO。Example Input
1 0 2 2 1 2 2 1
Example Output
YES NO
#include<stdio.h> #define N 1005 int Pic ; int Check ; int TP ; int rear=0; void InitPic(int n){ for(int i=0;i<n;i++){ int row,col; scanf("%d %d",&row,&col); Pic[row-1][col-1]=1; } } int CheckIng(int start,int n){ for(int i=0;i<n;i++){ if(Pic[i][start]&&i!=start){ return 0; } } return 1; } void DeletIng(int start,int n){ for(int i=0;i<n;i++){ Pic[start][i]=0; } } void TpSort(int n){ for(int j=0;j<n;j++){ for(int i=0;i<n;i++){ if(CheckIng(i,n)&&!Check[i]){ DeletIng(i,n); Check[i]=1; TP[rear++]=i; } } } } void Print(int n){ for(int i=0;i<n;i++){ printf("%d ",TP[i]+1); } printf("\n"); } void RollBack(int n){ for(int i=0;i<n;i++){ TP[i]=0; Check[i]=0; for(int j=0;j<n;j++){ Pic[i][j]=0; } } rear=0; } int main(){ int n,m; while(scanf("%d %d",&n,&m)!=EOF){ InitPic(m); TpSort(n); if(rear<n){ printf("NO\n"); } else{ printf("YES\n"); } //Print(n); RollBack(n); } return 0; }
相关文章推荐
- 图算法 图的表示(邻接表和邻接矩阵)和拓扑排序
- 图的邻接矩阵表示和拓扑排序
- 图(有向图,无向图)的邻接矩阵表示C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency matrix
- 图的数组表示(邻接矩阵)
- 7-1-图、表的数组(邻接矩阵)表示法-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构——图的邻接矩阵表示法
- 图的邻接矩阵表示
- 图的邻接矩阵表示法
- hdu1285(邻接矩阵写拓扑排序)
- 以邻接矩阵(数组表示)的方法来创建带权值的有向图(有向网)
- 数据结构——图的邻接矩阵表示法
- 拓扑排序(邻接矩阵)
- 大二用C++写的图邻接矩阵表示法
- 数据结构中用图的邻接矩阵的表示以及深度搜索
- 邻接矩阵和邻接表表示
- 图(2)—— 邻接矩阵表示法
- BZOJ 4484: [Jsoi2015]最小表示 拓扑排序 bitset
- 图的邻接矩阵表示
- 图的邻接矩阵表示法
- 图的深度优先遍历(用邻接矩阵表示图)