本文主要是介绍UVA 10305,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*简单的拓扑排序*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 200
int t, n, m, data[M][M];
int topo[M], c[M], data[M][M];
int dfs( int u )
{int v;c[u] = -1;for( v = 1; v <= n; v++ )if( data[u][v] ){if( c[v] < 0 )return 0;else if( !c[v] && !dfs(v) )return 0;}c[u] = 1; topo[t--] = u;return 1;
}
int main()
{int i, w, x, y, u;while( scanf( "%d%d", &n, &m ) ){if( m == 0 && n == 0 ) //因为可能n = 1; m = 0break; //最开始写的是whiel( scanf(n,m) && n, m ) WA!!memset(c, 0, sizeof(c) );memset(data, 0, sizeof(data) );while( m-- ){scanf( "%d%d", &x, &y );data[x][y] = 1;}t = n;for( u = 1; u <= n; u++ )if( !c[u] )dfs(u);for( i = 1; i <= n-1; i++ )printf( "%d ", topo[i] );printf( "%d\n", topo[n] );}return 0;
}
这篇关于UVA 10305的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!